JavaScript map, filter, find, and reduce: A Practical Guide with Examples

JavaScript map, filter, find, and reduce: A Practical Guide with Examples

JavaScript’s array methods — map, filter, find, and reduce — are the four functions that turn ten-line for loops into one expressive line. Once you internalize them, your code becomes shorter, more declarative, and easier for the next reader (often future-you) to understand.

This guide walks through each method with realistic examples, shows the modern arrow-function shorthand, and ends with a comparison table so you always know which one to pick.

The example dataset

We’ll reuse the same fruit dataset across every example:

const fruits = [
  { name: "Apple",  units: 3, price: 0.25 },
  { name: "Mango",  units: 6, price: 0.35 },
  { name: "Banana", units: 4, price: 0.15 },
];

map(): transform every item

map() returns a new array of the same length, where each element is the result of applying the callback. Use it whenever every input produces exactly one output.

When to use this: formatting, computing derived values, extracting fields, or projecting one shape into another.

const summary = fruits.map(fruit => {
  const cost = fruit.price * fruit.units;
  return `${fruit.units} units of ${fruit.name} costs $${cost.toFixed(2)}`;
});
// [
//   '3 units of Apple costs $0.75',
//   '6 units of Mango costs $2.10',
//   '4 units of Banana costs $0.60'
// ]

// Concise arrow-function form for one-line transforms:
const names = fruits.map(f => f.name);
// ['Apple', 'Mango', 'Banana']

filter(): keep only items that match

filter() returns a new array containing only the items for which the callback returns truthy.

When to use this: narrowing a list — active users, items in stock, errors above a severity, etc.

const acceptablePrice = 0.2;

const cheapFruits = fruits.filter(f => f.price < acceptablePrice);
// [{ name: 'Banana', units: 4, price: 0.15 }]

find(): return the first matching item

find() returns the first element that matches, or undefined if none does. Unlike filter, it stops searching as soon as it finds a match — significantly faster for large arrays.

When to use this: looking up a single item — a user by ID, the first error in a log, a config matching a key.

const search = "mango";
const mango = fruits.find(f => f.name.toLowerCase().includes(search));
// { name: 'Mango', units: 6, price: 0.35 }

// Need just the index? Use findIndex:
const idx = fruits.findIndex(f => f.name === "Mango"); // 1

reduce(): collapse the array into a single value

reduce() applies a two-argument callback (accumulator, currentItem) cumulatively across the array, returning a single result.

When to use this: totals, products, grouping by key, or building any single object from many. Always provide an explicit initial value as the second argument — it makes the reducer clearer and avoids surprises with empty arrays.

const grandTotal = fruits.reduce(
  (acc, fruit) => acc + fruit.price * fruit.units,
  0,
);
// 3.45

// reduce can also build objects — e.g., group by key:
const byName = fruits.reduce((acc, f) => {
  acc[f.name] = f;
  return acc;
}, {});
// { Apple: {...}, Mango: {...}, Banana: {...} }

Quick comparison

MethodReturnsStops early?Use when
mapArray (same length)NoTransforming each item
filterArray (≤ same length)NoKeeping items that match
findSingle item or undefinedYesFirst match only
reduceSingle value of any typeNoAggregating into one result

Chaining for power

These methods are chainable, which is where they really shine. The total cost of cheap fruits in one expression:

const cheapTotal = fruits
  .filter(f => f.price < 0.20)
  .map(f => f.price * f.units)
  .reduce((acc, cost) => acc + cost, 0);
// 0.60

Note: each method creates a new intermediate array, so for very large datasets consider a single reduce or a generator-based approach.

Final thoughts

Once map, filter, find, and reduce click, you’ll reach for them constantly. They make data transformations declarative — you describe what you want, not how to loop. That alone tends to eliminate a whole category of off-by-one and mutation bugs.

Looking for the Python equivalents? See Python useful list functions: map, filter, find and reduce. The full snippet for these examples is on GitLab.

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Pin It on Pinterest

0
Would love your thoughts, please comment.x
()
x