🧭Map, Filter and Reduce
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
Example code
javascript
[1, 2, 3, 4]
.filter((n) => n % 2 === 0)
.map((n) => n * 10)
.reduce((a, b) => a + b, 0); // 60Common mistake
Key takeaways
- 1Map changes. Same length out.
- 2Filter keeps. Only items that pass.
- 3Reduce folds. Many items, one value.