Describing the map
Operation
- The
map
operation is used for applying a function to each element of an array; -
It accepts a callback function with the following arguments:
currentValue:
Current element processed in the arrayindex
: Optional index ofcurrentValue
array
: Optional array that is calledthisArg
: Optional value to use asthis
Defining an Array
let fruits = ["Apple", "Banana"];
console.log(fruits.length);
// 2
Using the map
Operation
// Regular function
let f = fruits.map(function(item, index, array) {
return item + " Pie";
})
console.log(f);
// ["Apple Pie", "Banana Pie"]
// Arrow function
let f = fruits.map(item => item + " Pie");
console.log(f);
// ["Apple Pie", "Banana Pie"]
// forEach
let f = fruits.forEach(item => item + " Pie");
console.log(f);
// undefined
Previous
Next