Lodash _.transform() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

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])
CallWhat 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.

Library declarations
Lodash
_.transform(object, [iteratee], [accumulator])

Parameters

ParameterDescription
objectRequired. Object or array to iterate
iterateeOptional. (acc, value, key, collection) — mutate acc
accumulatorOptional. Seed result; defaults to a new empty object

Return Value

Return ValueDescription
*The final accumulator

Lodash Examples

Example 1 — Official: Array with Early Exit

Push squares until the iteratee returns false.

Lodash
_.transform([2, 3, 4], function (result, n) {
  result.push(n *= n);
  return n % 2 === 0;
}, []);
// → [4, 9]
Try It Yourself

How It Works

  1. Seed. Start with an empty array.
  2. Mutate. Square n and push into result.
  3. Break. Returning false stops before 4.
  4. Docs. Matches the official Lodash example.

Example 2 — Official: Group Keys by Value

Build a lookup of values → list of keys.

Lodash
_.transform({ a: 1, b: 2, c: 1 }, function (result, value, key) {
  (result[value] || (result[value] = [])).push(key);
}, {});
// → { "1": ["a", "c"], "2": ["b"] }
Try It Yourself

How It Works

  1. Seed. Empty object becomes the groups map.
  2. Bucket. Ensure result[value] is an array.
  3. Push. Store each key under its value.
  4. Result. 1 maps to a and c.

Example 3 — Filter Object Properties

Copy only keys that pass a condition — pickBy-style control.

Lodash
_.transform({ a: 1, b: 2, c: 3 }, function (result, value, key) {
  if (value > 1) {
    result[key] = value;
  }
}, {});
// → { b: 2, c: 3 }
Try It Yourself

How It Works

  1. Test. Keep values greater than 1.
  2. Copy. Assign matching keys onto the accumulator.
  3. Skip. a: 1 is never written.
  4. Alt. _.pickBy is shorter for simple predicates.

Example 4 — Rename Keys While Mapping

Uppercase keys and double values in one pass.

Lodash
_.transform({ a: 1, b: 2 }, function (result, value, key) {
  result[key.toUpperCase()] = value * 2;
}, {});
// → { A: 2, B: 4 }
Try It Yourself

How It Works

  1. New key. Write to key.toUpperCase().
  2. New value. Store value * 2.
  3. Old keys. Original keys are not copied.
  4. Why transform. mapValues alone cannot rename keys.

Example 5 — transform vs reduce

Same sum — different iteratee contracts.

Lodash
_.transform([1, 2, 3], function (acc, n) {
  acc.total += n;
}, { total: 0 });
// → { total: 6 }

_.reduce([1, 2, 3], function (acc, n) {
  return acc + n;
}, 0);
// → 6
Try It Yourself

How It Works

  1. transform. Mutate acc.total in place.
  2. reduce. Return the next number each step.
  3. Shape. transform keeps a rich object seed easily.
  4. Break. Only transform stops early with false.

Browser & Runtime Support

_.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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.transform Lodash 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.

Next: _.unset()

Remove a nested property path from an object.

_.unset() tutorial →

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.

10 people found this page helpful