Fundamentals
Arrays & array methods
The essential iteration methods: map, filter and reduce.
Arrays come with higher-order methods that replace most manual for loops with clear, declarative transformations.
map is a factory line reshaping each item, filter is a bouncer letting some through, and reduce is a cashier totalling everything into one receipt.
Key concepts
1
map returns a new array by transforming every element. filter returns a new array with only the elements that pass a test. reduce boils an array down to a single value (a sum, an object, another array) using an accumulator.
mapfilterreduce
2
All three are immutable — they return new arrays and leave the original untouched, which is why they pair so well with modern UI frameworks.
immutable
javascript
const nums = [1, 2, 3, 4];
nums.map(n => n * 2); // [2, 4, 6, 8]
nums.filter(n => n % 2 === 0); // [2, 4]
nums.reduce((sum, n) => sum + n, 0); // 10
// Chain them
const total = nums.filter(n => n > 1).map(n => n * 10).reduce((a, b) => a + b, 0); // 90