Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When should I use `map`, `filter`, or `reduce` on an array?

Quick answer

Use map when each input item should produce a corresponding transformed item; use filter when you want to keep only items that pass a test; use reduce when you want to accumulate items into one result, such as a total. With [3, 7, 10], these produce [6, 14, 20], [7, 10], and 20, respectively. [S1] [S2] [S3]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have an array of numbers, but I’m not sure which method fits which job. What’s the basic distinction?

Geez

Start by describing the shape of the result you want:

  1. A transformed value for each input: use map.
  2. Some of the original items, chosen by a test: use filter.
  3. One value built up from the items: use reduce.

For a shared example, assume the input is const numbers = [3, 7, 10];. Mapping each number to double it gives three results. Filtering for numbers above 5 keeps only the matching original numbers. Reducing by addition accumulates a total. These methods have different jobs: map constructs an array from callback return values, filter creates an array of items whose callback result is truthy, and reduce accumulates a result. [S1] [S2] [S3]

Follow-up question

Can you show the map and filter versions and what each one returns?

Geez

With the same numbers array, here are the two operations:

const numbers = [3, 7, 10];

const doubled = numbers.map(number => number * 2);
// [6, 14, 20]

const aboveFive = numbers.filter(number => number > 5);
// [7, 10]

Read the map callback as “turn each number into twice that number.” Each input has a corresponding returned value, so all three numbers contribute to the result. [S1]

Read the filter callback as “does this number pass the test?” Only numbers for which number > 5 is true are included. The result contains the original matching numbers, not their doubled versions. [S2]

A useful check: if you want to change values, reach for map; if you want to decide which items stay, reach for filter.

Follow-up question

Where does reduce fit? I want the total, not another array.

Geez

reduce fits when you want to build up a result as the array is processed. For this example, the accumulated result is a number: the sum.

const numbers = [3, 7, 10];

const total = numbers.reduce(
  (sum, number) => sum + number,
  0
);
// 20

Here, sum is the running total, number is the current item, and 0 is the starting value. The callback adds each item to the running total; the final result is 20, not an array. MDN’s example likewise uses reduce to accumulate cart costs into a total with an initial accumulator value. [S3]

If your intended answer is still a list of transformed values, map is a clearer fit. If it is a list containing only items that pass a test, use filter. Choose reduce when accumulation is the actual task.

Follow-up question

Is there a quick way to choose correctly when I’m writing code?

Geez

Name the result before choosing the method: “I need a transformed list,” “I need a subset,” or “I need one accumulated answer.” Then match it to map, filter, or reduce. For the sample array, here are all three results:

const numbers = [3, 7, 10];

const doubled = numbers.map(number => number * 2);
const aboveFive = numbers.filter(number => number > 5);
const total = numbers.reduce((sum, number) => sum + number, 0);

// doubled:    [6, 14, 20]
// aboveFive:  [7, 10]
// total:      20

The results follow from this input and these callbacks. If the desired result is unclear, write down a sample of what it should look like first. A transformed list, a smaller selected list, or one value usually makes the choice apparent.

A practical checklist

  • Write down the shape you want back: a transformed list, a selected list, or one accumulated result.

  • Use map when each input should produce its corresponding transformed value.

  • Use filter when a test decides which original items to keep.

  • Use reduce when a running accumulation produces the result you need.

  • Try the operation on a small example and compare its output with the result you intended.

A few more useful answers

Does `filter` change the values of the items it keeps?

In the example, filter selects the original numbers that pass its test, so [3, 7, 10] filtered for values above 5 gives [7, 10]. Use map when the goal is to transform each value. [S1] [S2]

Can `reduce` return a number instead of an array?

Yes. In the example, the callback accumulates a sum and the initial value is 0, so the result is the number 20. [S3]

What if I need to select items and then transform them?

Treat those as two distinct goals: first keep items that pass a test with filter, then transform the selected items with map. Check that the intermediate selected list is what you intend before using it.

Sources

References used for this guide.

  1. [S1] Array.prototype.map() — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] Array.prototype.filter() — JavaScript | MDN
    developer.mozilla.org · Checked
  3. [S3] Array.prototype.reduce() — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work