Member-only story

JavaScript — Filter, Map, Reduce

Get started with functional programming

Adi S
Better Programming
3 min readNov 13, 2019

--

Filter, map, and reduce are methods available to use to manipulate data in an array. Since JS introduced these methods in 2011, they’ve become some of the most common methods used for traversing arrays.

For years before 2011, developers would use for loops, and you still can today; however, these methods will help manage complexity and make your code more readable.

Filter

The filter method does what you would expect it to — it’ll filter out data from an array. For example, if you want to find a type of animal that’s longer than five characters:

Note: We’re passing in an arrow function into the filter method. The filter operator will normally accept the parameters (current item, index, and entire array).

Map

Map is used when you want to perform the same operation on every item in the array. After map transverses the array, it’ll return a new array with the translated elements.

Map accepts a callback function, and, like filter, it can accept the current item of the array, current index of the current itemm and the entire array. Here is an example to convert feet to yards:

--

--

No responses yet

Write a response