Arrays: map, filter, reduce
Most work with lists is one of three jobs: transform every item, keep some items, or turn the list into one value. map, filter and reduce do exactly these without manual loops, and they are used everywhere in React.
7.1 Why is it used?
- Getting the product names and the cart total with loops took nine lines and two extra variables.
- With a loop you manage the index yourself and start from an empty [] or 0.
- Reading the code you don't see the intention: are we building a list or a total?
- Built-in array methods say directly what you want to do.
JavaScript
const cart = [ { name: "Shoes", price: 1000 }, { name: "Bag", price: 500 }, { name: "Watch", price: 2000 },]; const names = [];for (let i = 0; i < cart.length; i++) { names.push(cart[i].name);}let total = 0;for (const item of cart) { total += item.price;}console.log(names, total);Output
> [ 'Shoes', 'Bag', 'Watch' ] 35007.2 map: transform every item
- Runs a function on every item and builds a new array from what it returns.
- The new array always has the same length as the original.
JavaScript
const cart = [ { name: "Shoes", price: 1000 }, { name: "Bag", price: 500 }, { name: "Watch", price: 2000 },]; const names = cart.map((item) => item.name);console.log(names);Output
> [ 'Shoes', 'Bag', 'Watch' ]7.3 filter: keep items by a condition
- Checks a condition on every item and keeps only those where it is true.
- Bag (500) is left out of price > 800; Shoes and Watch stay.
JavaScript
const costly = cart.filter( (item) => item.price > 800);console.log(costly);Output
> [ { name: 'Shoes', price: 1000 }, { name: 'Watch', price: 2000 } ]7.4 reduce: whole array, one value
- Takes a function and a starting value (0).
- At each step the function gets sum (the total so far) and the current item; what it returns becomes the next sum.
- 0 -> 1000 -> 1500 -> 3500.
JavaScript
const cart = [ { name: "Shoes", price: 1000 }, { name: "Bag", price: 500 }, { name: "Watch", price: 2000 },]; const total = cart.reduce( (sum, item) => sum + item.price, 0);console.log(total);Output
> 3500- Step 1
1.Start: sum = 0
- Step 2
2.Shoes: 0 + 1000 = 1000
- Step 3
3.Bag: 1000 + 500 = 1500
- Step 4
4.Watch: 1500 + 2000 = 3500
- Step 5
5.Result: 3500
7.5 Chaining: filter, map, reduce
- map and filter return new arrays, so they can be chained.
- Total of the costly products in one statement, no loop.
- Step 1
1.cart: Shoes 1000, Bag 500, Watch 2000
- Step 2
2.filter(price > 800): Shoes, Watch
- Step 3
3.map(item => item.price): [1000, 2000]
- Step 4
4.reduce(sum + price, 0): 3000
One statement, no manual loop
7.6 find, some, every, includes
JavaScript
const marks = [72, 45, 90, 38]; console.log(marks.find((m) => m < 40));console.log(marks.some((m) => m > 85));console.log(marks.every((m) => m >= 40));console.log(marks.includes(90));Output
> 38> true> false> true| Method | Returns | Example (marks) |
|---|---|---|
| map | New array, same length | Names from the cart |
| filter | New array of matching items | Items with price > 800 |
| reduce | One value | Cart total 3500 |
| find | First matching item (or undefined) | 38 (first below 40) |
| some | true if at least one passes | true (90 > 85) |
| every | true if all pass | false (38 < 40) |
| includes | true if the value is in the array | true (90) |
7.7 Checkpoint: default sort
- [10, 1, 5, 100].sort() gives [1, 10, 100, 5], not 1, 5, 10, 100.
- Default sort converts numbers to strings and compares them in dictionary order.
- For numbers always pass a compare function: (a, b) => a - b.
JavaScript
const nums = [10, 1, 5, 100];console.log(nums.sort());Output
> [ 1, 10, 100, 5 ]7.8 Mutating vs copy methods
- sort changes the original array itself, so prices got sorted too (a common React bug).
- toSorted (ES2023) returns a new sorted copy and leaves the original alone.
- with(index, value) (ES2023) returns a copy with one item updated; useful for React state later.
JavaScript
// Wrongconst prices = [500, 2000, 1000];const sorted = prices.sort((a, b) => a - b);console.log(prices);JavaScript
// Rightconst prices = [500, 2000, 1000];const sorted = prices.toSorted((a, b) => a - b);console.log(prices);JavaScript
const prices = [500, 2000, 1000]; const updated = prices.with(1, 1800);console.log(updated);console.log(prices);Output
> [ 500, 1800, 1000 ]> [ 500, 2000, 1000 ]| Mutating (changes original) | Non-mutating (returns new array) |
|---|---|
| push, pop, splice | map, filter, slice, concat |
| sort | toSorted |
| reverse | toReversed |
| arr[i] = value | with(i, value) |
7.9 Common mistakes
- Thinking forEach and map are the same: map returns a new array, forEach always returns undefined.
- Ignoring the result of map is a code smell; use forEach when you only want a side effect like logging.
- Sorting numbers without a compare function.
- Using sort / splice on React state instead of toSorted / with.
7.10 Interview tips
- map transforms every item and returns a new array of the same length.
- filter keeps items by a condition; reduce turns the whole array into one value.
- These three never change the original; sort and splice mutate it, so prefer toSorted and with for immutable updates.
- Default sort compares as strings: always pass (a, b) => a - b for numbers.