Member-only story
JavaScript Essential Concepts
. map ( ), .reduce( ), .filter( ) — one of the 40 Essentials of Javascript
JavaScript provides a range of powerful tools for manipulating and transforming data. This includes the map( ), reduce( ), and filter( ) methods, which are powerful tools for iterating through and transforming arrays. In this essay, we will look at the operation of each method, as well as two examples each of map( ), reduce( ), and filter( ).
Incase you prefer a video verson; please press here.
Map( )
The map( ) method is used to create a new array from an existing one by applying a function to each array item.
It takes two parameters: the array to be iterated over, and the function to execute on each item. The return value of the function is added to an output array.
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.map(num => num * num);
// Create a new array of objects using .map() and an array of data:
const data = [
{name: 'Alex', job: 'Engineer'},
{name: 'Bob', job: 'Doctor'}
];
const people = data.map((person, index) => {
const newPerson = {
id: index + 1,
name: person.name,
job: person.job
};
return newPerson;
});