Beginner · Arrays and objects · topic 17 of 61 · 30 min
Array methods: map, filter, reduce and friends
Explanation
Why use array methods?
You wrote loop patterns in module 2: the builder, the search, the accumulator. Array methods are those same patterns with a name. Instead of writing the loop, you say WHAT you want, and each method returns a result you can pass to the next one. This style is called declarative.
map: change every item
const pricesPaise = [19900, 4950, 120000];
const labels = pricesPaise.map((p) => `₹${(p / 100).toFixed(2)}`);
console.log(labels); // ['₹199.00', '₹49.50', '₹1200.00']filter: keep some items
const orders = [
{ id: 1, city: 'Pune', amount: 450 },
{ id: 2, city: 'Delhi', amount: 1200 },
{ id: 3, city: 'Pune', amount: 900 },
];
const pune = orders.filter((o) => o.city === 'Pune');
console.log(pune.map((o) => o.id)); // [1, 3]reduce: combine into one value
reduce walks the array with an accumulator. Your function receives the accumulator so far and the current item, and returns the new accumulator. The second argument of reduce is the starting value.
const total = orders.reduce((sum, o) => sum + o.amount, 0);
console.log(total); // 2550
const byCity = orders.reduce((acc, o) => {
acc[o.city] = (acc[o.city] ?? 0) + o.amount;
return acc;
}, {});
console.log(byCity); // { Pune: 1350, Delhi: 1200 }Searching: find, findIndex, findLast, some, every
const big = orders.find((o) => o.amount > 1000); // { id: 2, … } or undefined
const idx = orders.findIndex((o) => o.id === 3); // 2 or -1
const anyDelhi = orders.some((o) => o.city === 'Delhi'); // true
const allPaid = orders.every((o) => o.amount > 0); // trueFlattening: flat and flatMap
console.log([[1, 2], [3], [4, [5]]].flat()); // [1, 2, 3, 4, [5]]
console.log([[1, [2, [3]]]].flat(Infinity)); // [1, 2, 3]
const tags = ['red shirt', 'blue jeans'].flatMap((s) => s.split(' '));
console.log(tags); // ['red', 'shirt', 'blue', 'jeans']sort needs a compare function
By default, sort converts items to strings and sorts them alphabetically. That is why [10, 9, 1].sort() gives [1, 10, 9]. For numbers pass (a, b) => a - b for ascending or (a, b) => b - a for descending. For text, use (a, b) => a.localeCompare(b).
Chaining
const report = orders
.filter((o) => o.amount >= 500)
.map((o) => `#${o.id} ${o.city}`)
.join(', ');
console.log(report); // '#2 Delhi, #3 Pune'Grouping: Object.groupBy
ES2024 added Object.groupBy(list, fn), which groups items by the key your function returns: Object.groupBy(orders, (o) => o.city) gives { Pune: [...], Delhi: [...] }. It is in all current browsers and Node; in older environments use the reduce version above.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Array methods: map, filter, reduce and friends · 1/7
In this lesson you will process data with the most important array methods.
Prefer a video by a teacher? Find free videos on “javascript map filter reduce explained with examples” on YouTube ↗
In real life: Class results on a school portal
A school portal receives every student's marks. In three chained steps it keeps the students who passed, sorts them by marks, and builds the merit list text shown on the notice board.
const students = [
{ name: 'Asha', marks: 91 }, { name: 'Ravi', marks: 28 },
{ name: 'Meena', marks: 77 }, { name: 'Kabir', marks: 84 },
];
const merit = students
.filter((s) => s.marks >= 35)
.toSorted((a, b) => b.marks - a.marks)
.map((s, i) => `${i + 1}. ${s.name} (${s.marks})`);
console.log(merit);Practice (0/4 done)
Write doubleAll(nums) that returns a new array with every number doubled, using map.
Write cartTotal(items) that returns the sum of price * qty for every item, using reduce. Items look like { name, price, qty }.
Write expensiveNames(products, limit) that returns the names of products whose price is above limit, sorted A to Z.
Write countByCity(orders) that returns an object with the number of orders per city, using reduce. countByCity([{ city: "Pune" }, { city: "Pune" }, { city: "Goa" }]) returns { Pune: 2, Goa: 1 }.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Objects: keys and valuesArrays and objects · same module
- Destructuring, spread and restArrays and objects · same module
- More array powerFrontend Web Development · JavaScript in depth
- Arrays and array methodsFrontend Web Development · JavaScript basics
- Collection operationsKotlin and Android App Development · Classes, objects and collections
- Common loop patternsPython Programming · Loops: repeating work
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does [10, 9, 1].sort() give?
Answer: [1, 10, 9]
Without a compare function, items are compared as strings: "1" < "10" < "9".
What does [1, 2, 3, 4].filter((n) => n % 2).length give?
Answer: 2
n % 2 is 1 (truthy) for odd numbers: 1 and 3.
What does find return when nothing matches?
Answer: undefined
find returns undefined; findIndex returns -1.
What is [[1, 2], [3]].flat()?
Answer: [1, 2, 3]
flat removes one level of nesting by default.
[].reduce((a, b) => a + b) with no starting value does what?
Answer: Throws a TypeError
reduce of an empty array with no initial value throws a TypeError.
Which method does NOT change the original array?Related
Answer: toSorted()
toSorted returns a new sorted array. sort, reverse and splice all change the array they are called on.
From More array power
Which method returns a new array with only the items that pass a test?Related
Answer: filter
filter keeps the items for which the callback returns true.
Which operation turns List<Order> into Map<String, List<Order>>?Related
Why use break in a search loop?Related
Answer: To stop as soon as the item is found
Once found, looking further wastes time.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Arrays and objects” before its exam: Objects: keys and values, Destructuring, spread and rest, References, copies and equality.
- Module 4 exam: Arrays and objects · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Scope, this, prototypes and classes. 61 topics left in the course before the final exam.