JavaScript and TypeScript course ยท Module 5: Advanced JavaScript
Higher-Order Array Methods: map, filter, reduce
In this lesson7
The park vet asks for a report: the names of all carnivores, their total danger level and the average age of the herd. A plain for loop can do it, but every answer takes several lines of counters, conditions and temporary arrays. Now that you know the basic array methods, it's time to learn three of the most important higher-order methods, that is methods that take a function as an argument. They are the cornerstone of functional programming in JavaScript and incredibly useful when working with dinosaur data.
map() - Transforming Data
The map() method creates a new array by running a function on every element of the original array. It does not modify the original array. Whatever the function returns ends up in the result:
const dinosaurs = [
{ name: "Blue", age: 4, species: "Velociraptor" },
{ name: "Rex", age: 12, species: "T-Rex" },
{ name: "Bronto", age: 8, species: "Brachiosaurus" }
];
// Extract only the names
const names = dinosaurs.map(dino => dino.name);
console.log(names); // ["Blue", "Rex", "Bronto"]
// Transform into a new format
const reports = dinosaurs.map(dino => ({
label: dino.name + " (" + dino.species + ")",
ageInMonths: dino.age * 12
}));
console.log(reports);
// [{ label: "Blue (Velociraptor)", ageInMonths: 48 }, ...]The result always has as many elements as the original: three dinosaurs in, three reports out. The parentheses around the object literal in the second example are required, because without them JavaScript would treat the braces as the body of the arrow function.
filter() - Filtering Data
The filter() method creates a new array containing only the elements that satisfy the given condition (the function returns true). It works like a gate at the enclosure:
const dinosaurs = [
{ name: "Blue", age: 4, diet: "carnivore", dangerLevel: 8 },
{ name: "Rex", age: 12, diet: "carnivore", dangerLevel: 10 },
{ name: "Bronto", age: 8, diet: "herbivore", dangerLevel: 2 },
{ name: "Triki", age: 6, diet: "herbivore", dangerLevel: 3 }
];
// Filter carnivores
const carnivores = dinosaurs.filter(dino => dino.diet === "carnivore");
console.log(carnivores.length); // 2
// Dinosaurs with a high danger level
const dangerous = dinosaurs.filter(dino => dino.dangerLevel >= 8);
console.log(dangerous.map(d => d.name)); // ["Blue", "Rex"]Only the dinosaurs for which the function returned true pass through the gate. They are not copied - these are the same objects as in the original, gathered in a new array. When nobody meets the condition, you get an empty array, not undefined.
reduce() - Aggregating Data
The reduce() method reduces an array to a single value by processing elements one by one with an accumulator. The accumulator is the lab technician's notebook, and the second argument of reduce() is its starting state:
const dinosaurs = [
{ name: "Blue", age: 4 },
{ name: "Rex", age: 12 },
{ name: "Bronto", age: 8 },
{ name: "Triki", age: 6 }
];
// Sum of all ages
const totalAge = dinosaurs.reduce((sum, dino) => sum + dino.age, 0);
console.log(totalAge); // 30
// Average age
const avgAge = totalAge / dinosaurs.length;
console.log(avgAge); // 7.5The summing goes like this: 0 + 4, then + 12, + 8 and + 6, which gives 30. Always pass the starting value: without it reduce() takes the first element as the accumulator, here the whole Blue object, and instead of a number you get glued-together text. On an empty array without a starting value you get a TypeError.
The accumulator does not have to be a number. It can be an object to which we add dinosaurs by diet - that is how grouping works:
const dinosaurs = [
{ name: "Blue", age: 4, diet: "carnivore" },
{ name: "Rex", age: 12, diet: "carnivore" },
{ name: "Bronto", age: 8, diet: "herbivore" },
{ name: "Triki", age: 6 } // no diet recorded
];
// Grouping dinosaurs by diet
const grouped = dinosaurs.reduce((groups, dino) => {
const key = dino.diet || "unknown";
if (!groups[key]) groups[key] = [];
groups[key].push(dino.name);
return groups;
}, {});
console.log(grouped);
// { carnivore: ["Blue", "Rex"], herbivore: ["Bronto"], unknown: ["Triki"] }The empty object used as the starting value becomes a catalog of groups, and Triki, with no diet recorded, lands in the unknown group thanks to the || operator. Since ES2024 Object.groupBy() does the same in fewer lines - you will meet it in the lesson about the latest JavaScript features.
Chaining Methods into a Pipeline
The real power of these methods shows when we combine them into chains (chaining). Each method receives the result of the previous one, like a sample passed between lab stations:
const dinosaurs = [
{ name: "Blue", age: 4, diet: "carnivore", dangerLevel: 8 },
{ name: "Rex", age: 12, diet: "carnivore", dangerLevel: 10 },
{ name: "Bronto", age: 8, diet: "herbivore", dangerLevel: 2 },
{ name: "Triki", age: 6, diet: "herbivore", dangerLevel: 3 }
];
// Pipeline: filter carnivores -> extract names -> sort
const dangerousNames = dinosaurs
.filter(dino => dino.diet === "carnivore")
.map(dino => dino.name)
.sort();
console.log(dangerousNames); // ["Blue", "Rex"]
// Total danger level of carnivores
const totalDanger = dinosaurs
.filter(dino => dino.diet === "carnivore")
.reduce((sum, dino) => sum + dino.dangerLevel, 0);
console.log(totalDanger); // 18The order of the links matters: we filter first, so the later steps work on a smaller array. Watch out for sort(), because unlike the rest of the chain it mutates the array - here it is safe, since it sorts the fresh result of map(). Without a compare function it also sorts as text, so the numbers 9, 10 and 1 would end up as 1, 10, 9.
Searching and Checking
Not every question needs a new array. Sometimes you want to find one dinosaur or just check a condition - that is what the search and check methods are for:
const names = ["Blue", "Rex", "Bronto", "Triki"];
const levels = [8, 10, 2, 3];
console.log(names.indexOf("Rex")); // 1
console.log(names.includes("Delta")); // false
console.log(levels.find(level => level > 5)); // 8 - the first match
console.log(levels.some(level => level >= 10)); // true - at least one
console.log(levels.every(level => level > 0)); // true - all of themindexOf() returns a position or -1, includes() answers yes or no, and find() returns the first matching element or undefined. some() and every() stop working as soon as they know the answer.
flat() and flatMap() - Flattening
Data from the sectors often arrives as arrays inside arrays. The flat() method from ES2019 flattens them by one level, and flatMap() combines map() with that flattening:
const sectors = [["Blue", "Delta"], ["Rex"], ["Bronto", "Triki"]];
console.log(sectors.flat()); // ["Blue", "Delta", "Rex", "Bronto", "Triki"]
const zones = [
{ id: "A", dinosaurs: ["Blue", "Delta"] },
{ id: "B", dinosaurs: ["Rex"] }
];
const everyone = zones.flatMap(zone => zone.dinosaurs);
console.log(everyone); // ["Blue", "Delta", "Rex"]A plain map() would return an array of arrays here, while flatMap() gives a single list of residents right away. You can flatten deeper nesting by passing a depth, for example flat(2).
Function Composition: pipe and compose
A filter() and map() chain needs an array. Plain functions are combined through composition: pipe() passes a value through successive steps from left to right, and we will build it with reduce() itself:
const pipe = (...fns) => (value) => fns.reduce((acc, fn) => fn(acc), value);
const onlyCarnivores = list => list.filter(dino => dino.diet === "carnivore");
const toNames = list => list.map(dino => dino.name);
const carnivoreNames = pipe(onlyCarnivores, toNames);
console.log(carnivoreNames([
{ name: "Blue", diet: "carnivore" },
{ name: "Bronto", diet: "herbivore" }
])); // ["Blue"]Here the accumulator is the value itself, and each function is applied to the result of the previous one. compose() does the same from right to left, usually with reduceRight(). You will develop composition further in the location about functional programming.
This is the path to mastering array methods: basic manipulation (push, pop, shift, unshift), searching (indexOf, includes, find), transformation (map, filter, reduce), aggregation (every, some, flat), and at the top the compose and pipe patterns. In the lab below you will complete the reports following the TODO comments.
Remember: map() transforms, filter() sifts, and reduce() folds the whole herd into one result - together they form the lab's production line.
Code for this lesson: index.js
// Higher-order array methods: map, filter, reduce
const dinosaurs = [
{ name: "Blue", age: 4, diet: "carnivore", dangerLevel: 8 },
{ name: "Rex", age: 12, diet: "carnivore", dangerLevel: 10 },
{ name: "Bronto", age: 8, diet: "herbivore", dangerLevel: 2 },
{ name: "Triki", age: 6, diet: "herbivore", dangerLevel: 3 },
{ name: "Ptero", age: 5, diet: "carnivore", dangerLevel: 6 }
];
// === MAP ===
console.log("=== map() ===");
const names = dinosaurs.map(dino => dino.name);
console.log("Names:", names);
// TODO: Use map() to create an array of objects
// with the fields: label (name + species) and ageInMonths (age * 12)
// === FILTER ===
console.log("\n=== filter() ===");
const carnivores = dinosaurs.filter(dino => dino.diet === "carnivore");
console.log("Carnivores:", carnivores.map(d => d.name));
// TODO: Filter the dinosaurs with dangerLevel >= 7
// === REDUCE ===
console.log("\n=== reduce() ===");
const totalAge = dinosaurs.reduce((sum, dino) => sum + dino.age, 0);
console.log("Sum of ages:", totalAge);
console.log("Average age:", totalAge / dinosaurs.length);
// TODO: Use reduce() to calculate the total danger level
// === PIPELINE (method chaining) ===
console.log("\n=== Pipeline ===");
// TODO: Chain filter -> map -> reduce
// Filter the carnivores, take dangerLevel, add it up
const totalDangerOfCarnivores = dinosaurs
.filter(dino => dino.diet === "carnivore")
.map(dino => dino.dangerLevel)
.reduce((sum, level) => sum + level, 0);
console.log("Total danger of the carnivores:", totalDangerOfCarnivores);Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What does the map() method return in JavaScript?
2. What is the purpose of the filter() method in arrays?
These are 2 of 4 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Transform the Velociraptor array by adding a trainingComplete property to each one.
- Code editor
Filter the array to find only alpha Velociraptors.
- Click in order
Arrange the map method syntax in the correct order:
- Code editor
Calculate the average age of the pack using reduce().
- Click in order
Arrange the filter method syntax in the correct order:
- Vertical ordering
Order the array methods learning path from basic to advanced:
Tasks after this lesson
- QuestionAdvanced Iterations - Map
- QuestionAdvanced Iterations - Filter
- Coding taskRaptor Training Data
- QuestionAdvanced Iterations - Reduce
- Coding taskFiltering Alpha Raptors
- Click in orderSequence: Array map
- Coding taskPack Statistics
- Click in orderSequence: Array filter
- QuestionThe reduce Method in Data Pipelines
- Put in orderArray Methods Mastery Path