Map, filter & reduce in Javascript

January 11, 2019

Functional programming focuses heavily on lists and list operations. Within these operations 3 functions are very popular and re-used in almost every programming language.

Map (extract user UIDs):

const users = [
    {name: "John Smith", uid: 342341},
    {name: "Jane Doe", uid: 342341},
    {name: "David Berman", uid: 634634},
    {name: "John Lewinski", uid: 2123124}
];

const userUIDs = users.map(user => user.uid);

Filter (Grab only even numbers from array):

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const evenNumbers = numbers.filter(number => number % 2 === 0);

Reduce (Get the sum of array values):

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

const sumOfNumbers = numbers.reduce((accumulatedVal, number) => accumulatedVal + number, 0);