Skip to content
Arrays & ObjectsChapter 21 / 47Beginner5 min read

Array Methods: map, filter, reduce

Learn JavaScript map, filter, and reduce along with find, some, every, and Object.groupBy, the modern ways to transform data, with examples.

On this page

1. What are array methods?

map changes every item and returns a new array, filter keeps only the items that pass, and reduce folds the whole array into a single value. None of the three change the original array.

Alongside them, find, some, every, and the ES2024 Object.groupBy are also useful every day.

2. Why does it matter?

In a for loop you have to manage the counter, the empty array, and push yourself. Array methods state the intent of the work clearly.

for loopArray methods
Create an empty array, run a loop, pushOne line with map or filter
Risk of index mistakesNo need to think about indexes
You have to read it to understand what happensThe method name tells you the task
Risk of changing the original arrayA new array, the original stays safe

3. Where is it used?

TaskMethod
Adding tax to pricesmap
Showing only in-stock productsfilter
Calculating the cart totalreduce
Finding one user by IDfind
Is any item out of stock?some
Are all fields filled in?every
Grouping products by categoryObject.groupBy

4. How does it work?

MethodWhat the callback returnsResult
mapA new valueA new array of the same length
filtertrue / falseAn array that is shorter or equal in length
reduceA new accumulatorA single value
findtrue / falseThe first match or undefined
some / everytrue / falsetrue or false
A pipeline
An array of products
filter: only in-stock
map: extract just the price
reduce: add up all the prices
One number: the total

The reduce callback takes two things: the accumulator (the result so far) and the current item. The second argument is the starting value.

5. Code example

map, filter, reduce

js
const products = [
  { name: "Shirt", price: 1500, inStock: true, category: "clothes" },
  { name: "Shoes", price: 4000, inStock: false, category: "footwear" },
  { name: "Cap", price: 800, inStock: true, category: "clothes" },
];

const names = products.map((p) => p.name);
const available = products.filter((p) => p.inStock);
const total = available.reduce((sum, p) => sum + p.price, 0);

console.log(names);
console.log(available.length);
console.log(total);

find, some, every, groupBy

js
const cap = products.find((p) => p.name === "Cap");
const anyOut = products.some((p) => !p.inStock);
const allPriced = products.every((p) => p.price > 0);
const byCategory = Object.groupBy(products, (p) => p.category);

console.log(cap.price);
console.log(anyOut, allPriced);
console.log(Object.keys(byCategory));
console.log(byCategory.clothes.length);

How does the code work?

PartMeaning
map((p) => p.name)["Shirt", "Shoes", "Cap"]
filter((p) => p.inStock)Only Shirt and Cap
reduce((sum, p) => ..., 0)Starts at 0, total is 2300
find(...)The first match, the Cap object
some / everytrue and true
Object.groupByAn object with clothes and footwear keys

Interview tip

forEach returns nothing (undefined). Use map when you need a new array, and forEach when you only want a side effect (like logging). Also, always give reduce an initial value, because calling it on an empty array without one throws a TypeError.

6. Common mistakes

Mistake: Using map just as a loop

Why? map builds a new array, so if you do not use the result it wastes memory and confuses readers.

Better: Use forEach or for of for side effects, and map when you need a new array.

Mistake: Not giving reduce an initial value

Why? It throws an error on an empty array, and with an array of objects the first object becomes the accumulator.

Better: Always pass the second argument, like reduce((sum, p) => sum + p.price, 0).

Mistake: Using filter()[0] instead of find

Why? filter checks the whole array and builds a new array, when you only wanted the first item.

Better: Use find, which stops at the first match.

Mistake: Doing everything with reduce

Why? A complex reduce is hard to read when map, filter, or groupBy would do the same job clearly.

Better: First check whether a direct method exists. For grouping, Object.groupBy is better.

7. What to learn before and after

Suggested order
Arrays
Array Methods
Objects
Destructuring

Final quick revision

PointDetail
mapChange every item, same length
filterItems that pass the condition
reduceCombine into one value, give an initial value
findThe first match or undefined
some / everyAny one / all of them
Object.groupByGroups by key (ES2024)