map
One input item becomes one output item. The array length stays the same.
Three array methods, three different jobs: transform every item, keep selected items, or combine the array into one result.
mapOne input item becomes one output item. The array length stays the same.
filterEach item passes or fails a test. The result may contain fewer items.
reduceEach item updates an accumulator. The final result can be any value.
map calls your function once for each array item and stores every returned value in a new array.
const prices = [250, 400, 125];
const labels = prices.map(price => `₹${price}`);
console.log(labels);
// ["₹250", "₹400", "₹125"]
Use the value returned by the callback. A map callback that only performs a side effect usually means forEach or a loop would say what you mean more clearly.
filter expects a truthy or falsy result. Truthy keeps the original item. Falsy leaves it out.
const tasks = [
{ title: "Ship notes", done: true },
{ title: "Write tests", done: false },
{ title: "Fix typo", done: true }
];
const completed = tasks.filter(task => task.done);
// "Ship notes" and "Fix typo" remain
filter does not clone the objects it keeps. The result is a new array containing references to the same objects.
The accumulator is the running result. The callback returns the accumulator value for the next iteration.
0.const amounts = [250, 400, 125];
const total = amounts.reduce(
(sum, amount) => sum + amount,
0
);
console.log(total); // 775
| Iteration | sum | amount | Returned |
|---|---|---|---|
| 1 | 0 | 250 | 250 |
| 2 | 250 | 400 | 650 |
| 3 | 650 | 125 | 775 |
Pass an initial value. Without one, the first item becomes the accumulator and an empty array throws a TypeError.
map(item, index, array)
filter(item, index, array)
reduce(accumulator, item, index, array)
const steps = ["install", "configure", "run"];
const numbered = steps.map(
(step, index) => `${index + 1}. ${step}`
);
This pipeline keeps paid orders, extracts their amounts, then adds them. Read it in the same order the data moves.
const revenue = orders
.filter(order => order.status === "paid")
.map(order => order.amount)
.reduce((total, amount) => total + amount, 0);
A chain creates an intermediate array after filter and another after map. That is usually fine. For very large or performance-critical data, one loop or one carefully written reduce may avoid those allocations.
return with braces.items.map(item => { item.name }) returns an array of undefined. Add return item.name or remove the braces.
The methods create new arrays, but objects inside them remain shared. Return a copied object when you need an immutable update.
reduce for every problem.If map or filter names the operation directly, use it. Dense accumulator logic is harder to scan.
[1, 2, 3] into [2, 4, 6]?
map, because every input produces one transformed output.
[1, 2, 3, 4].filter(n => n % 2)
[1, 3]. Odd remainders are 1, which is truthy. Even remainders are 0, which is falsy.
reduce?
It defines the accumulator's starting type and value, and it lets an empty array reduce safely.