Stop 01 · Web & JavaScript FoundationsTopics · 7/10

Array Methods (map, filter, reduce)

Part of the MERN Stack Roadmap

In short

APIs send you arrays of data, and React renders arrays of components. map, filter and reduce are the three tools you will reach for in almost every MERN file.

map transforms each item, filter keeps the items that match a test and reduce combines items into one value. React uses map to render lists, and you will use all three to shape API data.

The jaadu washes a line of tomatoes one by one, tosses a bruised one aside, then places the rest on a big brass weighing scale.

Think of it like this

The sabzi mandi routine

You bring a basket of vegetables home. First you wash each one (map: same count, each changed). Then you throw out the bruised ones (filter: fewer items, unchanged). Finally you weigh what's left to get one total (reduce: many in, one out).

Watch it happen

Shaping a cart of prices

120
0
40
1
350
2
80
3

1/4A cart with four item prices in rupees.

The big ideas

What you need to know

map transforms

Returns a new array of the same length, with your function applied to each item. React lists are built with map.

filter selects

Returns a new array with only the items for which your function returns true.

reduce combines

Walks the array carrying an accumulator, and returns one value such as a total, a count or an object.

No mutation

All three return new arrays or values and leave the original untouched. That matters for React state.

See it in code

Walk through the code

js
1const orders = [2  { id: 1, customer: "Aarav", amount: 450, status: "paid" },3  { id: 2, customer: "Meera", amount: 1200, status: "pending" },4  { id: 3, customer: "Kabir", amount: 800, status: "paid" },5];6 7const paid = orders.filter((o) => o.status === "paid");8const names = paid.map((o) => o.customer);9const revenue = paid.reduce((sum, o) => sum + o.amount, 0);10 11console.log(names);   // ["Aarav", "Kabir"]12console.log(revenue); // 1250

1/4An array of order objects, like what an API returns.

The same pattern you will use on API responses: filter what matters, map it to the shape you need, reduce it to totals.

Save yourself the pain

Common mistakes

Oops

Using map when you only want to loop and log, ignoring the returned array.

Do this instead

Use forEach (or for...of) for side effects. Use map only when you need the new array.

Oops

Calling reduce without an initial value on a possibly empty array.

Do this instead

Always pass the second argument, like reduce(fn, 0). An empty array with no initial value throws a TypeError.

Your mission

Crunch a student marks list

~20 min

Quick check

What does [3, 8, 12].filter((n) => n > 5).map((n) => n * 2) return?

Go deeper

  1. 1Take an array of orders and use filter to keep only paid ones, then reduce to total the amount in rupees.
  2. 2Use map to turn an array of user objects into an array of names.
  3. 3Learn find, some and every too, since they replace many manual loops.

Also part of these roadmaps

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge