By the end of this tutorial, you’ll use _.transform() to build new objects and aggregates with a mutable accumulator.
01
Core syntax
_.transform(object, iteratee, accumulator)
02
Accumulator
Mutate result object or custom seed.
03
Map + filter
Rename keys, double values, or skip entries.
04
Early exit
Return false to stop iterating.
05
vs reduce
In-place accumulator vs return-each-step.
06
Arrays too
Sum, group, or reshape list data.
Fundamentals
What Is _.transform()?
_.transform() is Lodash’s object-friendly alternative to Array.prototype.reduce. You pass a collection, a callback, and an optional accumulator; Lodash walks own enumerable keys and lets you build a new result by mutating that accumulator each step.
💡
One loop, many outcomes
Map keys, filter values, rename properties, or aggregate totals—all in a single pass without chaining multiple Lodash calls.
Use it for data normalization, conditional filtering, grouping, and summing arrays. When every key stays and only values change, _.mapValues() may be simpler; when you need full control, reach for _.transform().
Foundation
📝 Syntax
The signature takes a collection, iteratee, and optional starting accumulator:
javascript
_.transform(object, [iteratee], [accumulator])
Syntax Rules
object — object or array to iterate (own enumerable keys).
Lodash uses your seed or creates a fresh {} for object input.
Setup
2
Iterate collection
Each own enumerable key invokes the iteratee with accumulator, value, key, and collection.
Loop
3
Mutate or break
You update the accumulator in place. Return false to stop early.
Build
=
📈
Result ready
The final accumulator is returned; the source collection is unchanged.
Important
📝 Notes
_.transform() does not mutate the source object or array—only the accumulator.
Omit the accumulator to start with an empty object {} for object inputs.
Return false from the iteratee to stop iteration early.
Works on arrays too—pass a custom seed like { total: 0 } or [].
For value-only mapping with the same keys, _.mapValues() is often clearer.
Iteration covers own enumerable keys only (not inherited prototype properties).
Wrap Up
Conclusion
_.transform() gives you one flexible loop to map, filter, rename, and aggregate object or array data into a new accumulator. Pair it with a clear seed value and mutate the accumulator inside the callback.
When you only need to change values in place, try _.mapValues(). When you only need conditional filtering, _.pickBy() may suffice. Next in the series: _.unset() for removing nested paths.
Pass an explicit accumulator when you need arrays or numeric totals
Mutate the accumulator inside the iteratee—that is the intended pattern
Return false to stop early when you have enough data
Use _.mapValues() when keys stay the same and only values change
Keep iteratee logic small; extract helpers for complex nested transforms
❌ Don’t
Expect the iteratee return value to become the next accumulator (use _.reduce() instead)
Forget to initialize numeric fields like { total: 0 } before summing
Mutate the source object inside the callback—build a separate accumulator
Reach for _.transform() when _.pickBy() or _.mapValues() already fits
Assume inherited prototype keys are included—iteration is own enumerable only
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.transform()
Use these when one pass should map, filter, and aggregate.
5
Core concepts
📈01
Accumulator
Mutate in place.
Core
🔄02
One pass
Map + filter.
Pattern
🔀03
vs reduce
Return vs mutate.
Compare
⏹️04
Early exit
Return false.
Tip
📦05
Source safe
Read-only input.
Note
❓ Frequently Asked Questions
_.transform() iterates over an object or array and builds an accumulator by running a callback on each element. You typically mutate the accumulator inside the callback and receive it back at the end.
Both fold a collection into one value. _.reduce() expects the iteratee to return the next accumulator each step. _.transform() expects you to mutate the accumulator in place; returning false stops iteration early.
No. The source collection is read-only. You choose whether to mutate the accumulator object or array you pass in (or the default empty object Lodash creates).
If you omit the accumulator, Lodash starts with an empty object {} for object inputs. For arrays, pass your own accumulator explicitly (for example [] or { total: 0 }).
Yes. Only copy keys into the accumulator when your condition passes—similar to _.pickBy() but with full control in one loop.
Use _.mapValues() when every key is kept and only values change. Use _.transform() when keys may be renamed, dropped, or when you need array-style aggregation.
Did you know?
_.transform() is the Lodash method behind many “map and filter in one loop” recipes. Unlike Array.prototype.reduce(), you mutate the accumulator instead of returning it each step—and returning false breaks out early, which plain reduce does not offer out of the box.