02 Array Map Reduce

postImage


02 - .map() & .reduce()

JAVASCRIPT
const myArray = [1,2,3,4,5,6];

Use .map() for applying a transformation on each elements

JAVASCRIPT
const mutipliedArray = myArray.map((element) => element*2);
console.info(mutipliedArray); // [ 2, 4, 6, 8, 10, 12 ]

Use .reduce() for merging together each element of the array

JAVASCRIPT
const sumOfArray = myArray.reduce((previous, current) => previous+current);
console.info(sumOfArray); // 21

These methods can be chained together

JAVASCRIPT
const sumOfMultipliedArray = myArray.map((el) => el*2).reduce((prev, cur) => prev+cur);
console.info(sumOfMultipliedArray); // 42

profileImage