GUIDE深度 · Deep dive
Reading map, filter and reduce
map returns a new array of the same length, filter returns a subset, and reduce returns one accumulated value. Read callback arguments (value, index, array), then the return value; chains run left to right.
Three methods
map: transform each element into a new value.
filter: keep elements whose callback is true.
reduce: combine into one value with an accumulator.
Chaining
[1,2,3].map(x=>x*2).filter(x=>x>2) -> [4,6].
sort mutates in place; slice does not.
FAQ
Does map mutate?
No; it returns a new array.
Does reduce's initial value matter?
Yes; without it the first item is the initial value, and an empty array throws.