map, filter, reduce
Array Transformations
Master JavaScript's three most powerful array methods to transform, select, and combine data in clean, declarative pipelines.
Why map, filter, and reduce?
Traditional for loops mix iteration logic with transformation logic. That makes code harder to read, test, and compose. These three methods separate the two concerns:
The Data Pipeline Model
You can chain these methods left-to-right. Data flows through each stage: map transforms every item, filter keeps only what you need, and reduce collapses the result into one value.
The Callback Pattern
All three methods accept a callback function: a small function you write that gets called once per element. JavaScript handles the loop for you.
.map(): Transform Every Element
.filter(): Keep or Discard
.reduce(): Collapse to One Value
map vs filter vs reduce
| .map() | .filter() | .reduce() | |
|---|---|---|---|
| Purpose | Transform each item | Select items by test | Combine into one value |
| Callback returns | New value | Boolean | Updated accumulator |
| Output type | Array (same length) | Array (<= length) | Any type |
| Needs initial value? | No | No | Recommended: yes |
| Mutates original? | No | No | No |
How to Choose and Apply
.map() / Select → .filter() / Combine → .reduce().filter().map()Example 1: .map() in Action
Convert an array of temperatures from Celsius to Fahrenheit using the formula \( F = C \times 1.8 + 32 \).
Example 2: .filter() in Action
Keep only the students who scored 70 or above on a test.
Example 3: .reduce() in Action
Calculate the total price of items in a shopping cart.
Chaining Methods Together
Find the total cost of items priced over $10, with a 10% discount applied.
Arrow Functions: Shorter Callbacks
Arrow functions (=>) let you write the same callbacks in fewer characters. Both versions below do the exact same thing:
x => x * 2x => x + 1Common Mistakes
return, .map() produces an array of undefined. Arrow functions with braces also need an explicit return.null or undefined from .map() doesn't remove items: it keeps them as undefined. Use .filter() instead..reduce() uses the first element as the accumulator, which causes errors on empty arrays.Question 1 of 5
What does [1, 2, 3].map(x => x * 3) return?
.map() applies the callback x => x * 3 to each element: 1*3=3, 2*3=6, 3*3=9. It returns a new array of the same length.Question 2 of 5
Which method should you use to remove items from an array based on a condition?
.filter() tests each element against a condition and keeps only those that return true. It is the correct choice for removing unwanted items from an array.Question 3 of 5
What does [4, 5, 6].reduce((acc, x) => acc + x, 10) return?
Question 4 of 5
Consider this code:
const result = [10, 20, 30, 40, 50]
.filter(n => n > 20)
.map(n => n / 10);
What is the value of result?
.filter(n => n > 20) keeps [30, 40, 50]. Then .map(n => n / 10) transforms each: 30/10=3, 40/10=4, 50/10=5. Result: [3, 4, 5].Question 5 of 5
A developer writes nums.map(x => { x * 2 }) and gets [undefined, undefined, undefined]. What is the bug?
return x * 2; inside the braces, or remove the braces for an implicit return: x => x * 2.