Lodash _.reduce() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

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 chained map/filter reads 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 reduce or 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

javascript
_.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.
1

Sum an array of numbers

Start from zero and accumulate each element—classic fold intuition.

javascript
import reduce from "lodash/reduce";

reduce([1, 2, 3], (sum, n) => sum + n, 0);
// → 6
Try it Yourself
2

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.

javascript
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 }
Try it Yourself
3

Fold values from a plain object

Iteratee receives each numeric value—keys stay available if you need conditional logic.

javascript
import reduce from "lodash/reduce";

reduce(
  { a: 1, b: 2, c: 3 },
  (sum, value) => sum + value,
  0
);
// → 6
Try it Yourself

📋 _.reduce vs reduceRight, transform, map

APITraversalBest when
_.reduce(collection, iteratee, acc)Left → rightGeneral folds into arbitrary accumulators
_.reduceRight(collection, iteratee, acc)Right → leftOrder-sensitive folds (e.g. list prepending)
_.transform(object, iteratee, acc)Object/array aware mutationsYou mutate accumulator in-place per Lodash recipe
_.map + _.sum helpersPipeline stagesComposable readability beats custom folds

Pitfalls to avoid

Empty

Missing accumulator

Empty arrays with implicit seeds throw or surprise—always pass 0, [], or {} when emptiness is plausible.

Async

Await inside iteratee

Reduce does not await promises—use explicit async pipelines.

Readability

Over-fold

If logic reads like nested condition soup, split into named helpers or Lodash builtins.

❓ FAQ

Not directly—it folds into whatever accumulator you return—but your iteratee may mutate that accumulator if you reuse references.
Lodash calls it as (accumulator, value, index|key, collection) per visited entry.
reduce walks forward; reduceRight walks backward—critical when operations are not commutative.
Sometimes—for arrays Lodash can seed from index 0—but explicit seeds avoid empty-collection surprises.
Yes—enumerable values fold with keys passed as the third argument.

Summary

Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful