Grouping items by a key
The accumulator has to be returned by hand on every step. Leave out return groups and the second item crashes with a TypeError.
const byRole = users.reduce((groups, user) => { (groups[user.role] ??= []).push(user); return groups;}, {});const byRole = Object.groupBy(users, (user) => user.role);Object.groupBy, standard since ES2024. Use Map.groupBy when the keys aren’t strings.
Counting occurrences
A plain object inherits keys from Object.prototype, so counting the word “constructor” starts from a function instead of zero.
const counts = words.reduce((acc, word) => { acc[word] = (acc[word] || 0) + 1; return acc;}, {});// counts.constructor: "function Object() { [native code] }1"const counts = new Map();for (const word of words) { counts.set(word, (counts.get(word) ?? 0) + 1);}for…of and Map, standard since ES2015. A loop reads top to bottom, and nothing has to be handed back each time.
Looking items up by id
Spreading the accumulator copies every key it already holds on each step. For 10,000 users, that’s about 50 million copies to build one object.
const byId = users.reduce( (acc, user) => ({ ...acc, [user.id]: user }), {},);const byId = Object.fromEntries( users.map((user) => [user.id, user]),);Object.fromEntries, standard since ES2019. Pass the same pairs to new Map() when the keys aren’t strings.
Checking whether every item passes
The answer is settled at the first unpaid invoice, but reduce keeps calling back for every one after it. The intent hides inside ok &&.
const allPaid = invoices.reduce( (ok, invoice) => ok && invoice.paid, true,);const allPaid = invoices.every((invoice) => invoice.paid);every and some, standard since ES5. Both stop at the first item that decides the answer.
Flattening nested arrays
concat builds a fresh array on every step, so the work grows with the square of the input.
const tags = posts.reduce( (all, post) => all.concat(post.tags), [],);const tags = posts.flatMap((post) => post.tags);flatMap and flat, standard since ES2019.
Filtering and transforming in one pass
This shape exists to skip the extra array that .filter().map() creates. Iterator helpers skip it too, and every step keeps its name.
const emails = users.reduce((acc, user) => { if (user.active) acc.push(user.email); return acc;}, []);const emails = users .values() .filter((user) => user.active) .map((user) => user.email) .toArray();Iterator helpers, standard since ES2025. Each user goes through filter and map before the next one starts.
Removing duplicates
includes scans the whole accumulator for every item, then the spread copies it all over again.
const unique = ids.reduce( (acc, id) => (acc.includes(id) ? acc : [...acc, id]), [],);const unique = [...new Set(ids)];Set, standard since ES2015. To compare lists, Set has union, intersection and difference, standard since ES2025.
Finding the largest value
Without an initial value, reduce throws on an empty array. With one, you have to invent a number that can never win.
const highest = scores.reduce( (max, score) => (score > max ? score : max),);const highest = Math.max(...scores);Math.max, standard since ES1. An empty array gives -Infinity. Past roughly 100,000 items, spreading can hit the engine’s argument limit, so use a loop there.
Running async work in order
Forget await previous and every request starts at once. The responses are thrown away either way.
await urls.reduce(async (previous, url) => { await previous; await fetch(url);}, Promise.resolve());const responses = await Array.fromAsync( urls, (url) => fetch(url),);Array.fromAsync, standard since ES2026. Each fetch finishes before the next one starts, and you keep the results.
Not on the list? Write the loop.
If no method name says what your callback does, a for…of loop will. It reads one line at a time, and break, continue and await work in it without tricks.
To keep reduce out of a codebase, turn on unicorn/no-array-reduce in ESLint.