Skip to content
BENIBIX Learn

Map, Filter and Reduce

Development🧭 Programming ParadigmsFunctional Programming

Also known as Map Filter Reduce, map(), filter(), reduce()

Three list tools. Map changes each item, filter keeps some, reduce makes one value.

Think of it like

Think of it like sorting fruit. Peel each one, keep the ripe ones, then blend them into one drink.

Example

Lucy keeps the even numbers from 1 to 4, makes each ten times bigger, then adds them up. The answer is 60.

Why it matters

These three show up in nearly every language. Reading them fast is a basic skill.

Definition

Map, filter and reduce work through a list without a loop. Map changes every item and gives back a list of the same length. Filter keeps only the items that pass a test. Reduce folds the whole list into one value, like a total. Chained together, they turn many lines of loop code into one readable line.

Example code

javascript
[1, 2, 3, 4]
  .filter((n) => n % 2 === 0)
  .map((n) => n * 10)
  .reduce((a, b) => a + b, 0); // 60
Keep 2 and 4, make them 20 and 40, add them to get 60.

Common mistake

Using map just to run something on each item and ignoring the new list. Use a plain loop or forEach for that.

Key takeaways

  1. 1
    Map changes. Same length out.
  2. 2
    Filter keeps. Only items that pass.
  3. 3
    Reduce folds. Many items, one value.