An alternative to _.reduce for transforming objects and arrays: you mutate an accumulator in the iteratee. Return false to stop early. Part of Lodash 4.x Object (since 1.0.0).
Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.
Syntax
Call _.transform. Start with this line.
Lodash
_.transform(object, [iteratee], [accumulator])
Call
What it does
_.transform(object, [iteratee], [accumulator])
Primary signature.
_.transform(object)
Work with an object’s keys, values, or paths.
Prefer import transform from "lodash/transform" when you want a smaller bundle.
_.transform is a Lodash Object helper (since 1.0.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Closest natives are Array.prototype.reduce / a manual for...in loop; Lodash adds a mutable-accumulator API and early exit via return false.
✓ Stable · Lodash 4.x
_.transform()
Available in [email protected] full builds and the lodash/transform modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.
100%With Lodash 4.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.transformLodash 4.x+
Bottom line: Safe for production once Lodash is loaded. Prefer import transform from "lodash/transform" for smaller builds. Ideal for one-pass map/filter/group builds with optional early exit.
Frequently Asked Questions
_.transform(collection, iteratee, accumulator) walks own enumerable keys/elements and lets you build a result by mutating the accumulator. The final accumulator is returned.
reduce expects you to return the next accumulator each step. transform expects you to mutate the accumulator; return false to stop early.
No. Only the accumulator is meant to change. The source collection is read.
If omitted, Lodash creates a new empty object for object-like sources (clone of an empty object of the same kind). For arrays, pass [] yourself when you need an array result.
When every key is kept and only values change. Use transform when keys may be renamed, dropped, grouped, or when you need early exit.
Use import transform from "lodash/transform" or require("lodash/transform"). Full build: import _ from "lodash" then _.transform.