JavaScript map filter & reduce

Array.map

var new_array = arr.map((element, index, array) => {
// Return value for new_array
}[, thisArg])

Array.filter

const numbers = [1, 2, 3, 4];
const evens = numbers.filter(item => item % 2 === 0);
console.log(evens); // [2, 4]
item % 2 === 0

Array.reduce

arr.reduce(reducerFunction, initialValue])
const numbers = [10, 20, 30, 40];
const sum = numbers.reduce(function (result, item) {
return result + item;
}, 0);
console.log(sum); // 100
const nums = [1,2,3,4];nums.reduce((a, c, i, arr) => arr[i] += a, 0);console.log(nums); // [1,3,6,10]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store