Lodash _.reduce() method
What you’ll learn
- How
_.reduce(collection, iteratee, accumulator)folds collections into totals, maps, or custom structs. - The iteratee argument order Lodash uses versus some other libraries.
- When
reduceRight,transform, or chainedmap/filterreads clearer. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.map() for iteratee ergonomics and _.partition() when you only need boolean splits—not arbitrary folds.
- You have folded arrays before with native
reduceor loops. - You can open Try-it labs or run snippets locally.
Overview
_.reduce is the escape hatch when no specialized Lodash helper fits—sums, maps-from-lists, merging configs, all share the same fold pattern.
Flexible accumulator
Numbers, objects, maps—anything you return flows forward.
Unified collections
Arrays and plain objects share one API surface.
Mind the seed
Explicit initials dodge empty-input edge cases.
Syntax
_.reduce(collection, iteratee, [accumulator]) - collection: array or plain object Lodash can iterate.
- iteratee:
(accumulator, value, index|key, collection)returning the next accumulator. - accumulator (optional): starting value; omit only when you accept Lodash default seeding rules.
- Returns: final accumulator after the last iteration.
Sum an array of numbers
Start from zero and accumulate each element—classic fold intuition.
import reduce from "lodash/reduce";
reduce([1, 2, 3], (sum, n) => sum + n, 0);
// → 6 Build a lookup object from rows
Fold into an empty object—often an alternative mindset to keyBy when logic isn’t a simple property read.
import reduce from "lodash/reduce";
reduce(
[
{ key: "usd", rate: 1 },
{ key: "eur", rate: 0.92 }
],
(acc, row) => {
acc[row.key] = row.rate;
return acc;
},
{}
);
// → { usd: 1, eur: 0.92 } Fold values from a plain object
Iteratee receives each numeric value—keys stay available if you need conditional logic.
import reduce from "lodash/reduce";
reduce(
{ a: 1, b: 2, c: 3 },
(sum, value) => sum + value,
0
);
// → 6 📋 _.reduce vs reduceRight, transform, map
| API | Traversal | Best when |
|---|---|---|
_.reduce(collection, iteratee, acc) | Left → right | General folds into arbitrary accumulators |
_.reduceRight(collection, iteratee, acc) | Right → left | Order-sensitive folds (e.g. list prepending) |
_.transform(object, iteratee, acc) | Object/array aware mutations | You mutate accumulator in-place per Lodash recipe |
_.map + _.sum helpers | Pipeline stages | Composable readability beats custom folds |
Pitfalls to avoid
Missing accumulator
Empty arrays with implicit seeds throw or surprise—always pass 0, [], or {} when emptiness is plausible.
Await inside iteratee
Reduce does not await promises—use explicit async pipelines.
Over-fold
If logic reads like nested condition soup, split into named helpers or Lodash builtins.
❓ FAQ
Summary
- Purpose:
_.reduce(collection, iteratee, accumulator)folds a collection into one accumulated result. - Contrast: prefer specialized helpers (
sum,keyBy) before writing bespoke folds. - Next: Lodash _.reduceRight(), Lodash _.partition() (previous), or collection hub.
Supply an explicit accumulator when empty collections are possible—without it Lodash uses the first element as the seed for arrays, which disappears when the collection is empty.
6 people found this page helpful
