Lodash _.updateWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

This method is like _.update except that it accepts a customizer which is invoked to produce the objects of path. If customizer returns undefined, path creation is handled by the method instead. Part of Lodash 4.x Object (since 4.6.0).

Pass the object and path or keys shown below. Check whether the helper mutates or returns a new object.

Syntax

Call _.updateWith. Start with this line.

Lodash
_.updateWith(object, path, updater, [customizer])
CallWhat it does
_.updateWith(object, path, updater, [customizer])Primary signature.
_.updateWith(object)Work with an object’s keys, values, or paths.

Prefer import updateWith from "lodash/updateWith" when you want a smaller bundle.

Library declarations
Lodash
_.updateWith(object, path, updater, [customizer])

Parameters

ParameterDescription
objectRequired. Target object (mutated)
pathRequired. Dot/bracket string or key array
updaterRequired. (value) => newValue for the leaf
customizerOptional. Creates missing intermediate containers

Return Value

Return ValueDescription
*The same object reference

Lodash Examples

Example 1 — Official: Object Customizer

Force nested objects for index-looking keys.

Lodash
var object = {};

_.updateWith(object, "[0][1]", _.constant("a"), Object);
// → { "0": { "1": "a" } }
Try It Yourself

How It Works

  1. Path. [0][1] looks like nested array indexes.
  2. Customizer. Passing Object builds plain objects.
  3. Updater. _.constant("a") sets the leaf to "a".
  4. Result. Keys are strings "0" and "1".

Example 2 — Default Arrays vs Object

See how the customizer changes the shape of missing segments.

Lodash
var a = {};
var b = {};

_.updateWith(a, "[0][1]", _.constant("x"));
// arrays for index-like keys

_.updateWith(b, "[0][1]", _.constant("x"), Object);
// objects for those keys

JSON.stringify(a);
// → {"0":[null,"x"]}   (shape may vary by engine fill)

JSON.stringify(b);
// → {"0":{"1":"x"}}
Try It Yourself

How It Works

  1. Default. Index-like keys prefer arrays.
  2. Object. Forces nested plain objects instead.
  3. Leaf. Updater still decides the final value.
  4. Why. Same path, different container types.

Example 3 — Increment with Object Containers

Updater transforms the leaf; customizer only shapes the path.

Lodash
var object = {};

_.updateWith(object, "0.1", function (n) {
  return (n || 0) + 1;
}, Object);

object;
// → { "0": { "1": 1 } }
Try It Yourself

How It Works

  1. Missing. Path does not exist → updater gets undefined.
  2. Guard. (n || 0) + 1 becomes 1.
  3. Containers. Object builds { "0": { "1": … } }.
  4. Split roles. Customizer ≠ updater.

Example 4 — Customizer Returns undefined

Returning undefined restores Lodash’s default path creation.

Lodash
var object = {};

_.updateWith(object, "a.b", _.constant(1), function () {
  return undefined; // use defaults
});

object;
// → { a: { b: 1 } }
Try It Yourself

How It Works

  1. Hook. Customizer is still called for missing segments.
  2. undefined. Signals “use Lodash defaults.”
  3. Default. Non-index key a becomes {}.
  4. Leaf. Updater sets b to 1.

Example 5 — updateWith vs update

Reach for updateWith only when container shape matters.

Lodash
var plain = {};
var forced = {};

_.update(plain, "[0][1]", _.constant("a"));
_.updateWith(forced, "[0][1]", _.constant("a"), Object);

// plain  → nested arrays (default)
// forced → { "0": { "1": "a" } }
Try It Yourself

How It Works

  1. update. Enough when defaults are fine.
  2. updateWith. Needed to force object (or other) containers.
  3. Same updater. Leaf logic is identical.
  4. Twin. Mirrors set / setWith.

Browser & Runtime Support

_.updateWith is a Lodash Object helper (since 4.6.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. It pairs _.update with the customizer hook from _.setWith; there is no single native equivalent.

✓ Stable · Lodash 4.x

_.updateWith()

Available in [email protected] full builds and the lodash/updateWith 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
_.updateWith Lodash 4.6+

Bottom line: Safe for production once Lodash is loaded. Prefer import updateWith from "lodash/updateWith" for smaller builds. Use it when missing path segments must be objects (or other custom containers).

Frequently Asked Questions

Like _.update: it runs an updater on the value at path and writes the result back. It also accepts a customizer that builds missing intermediate containers along the path.
update uses Lodash defaults for missing segments. updateWith lets you customize those containers — same idea as set vs setWith.
It creates missing intermediates: (nsValue, key, nsObject) => container. It does not transform the leaf — the updater does.
Lodash falls back to defaults: numeric indexes become arrays; other keys become plain objects.
Yes. It mutates in place and returns the same object. Clone first when immutability matters.
Use import updateWith from "lodash/updateWith" or require("lodash/updateWith"). Full build: import _ from "lodash" then _.updateWith.

Next: _.values()

Extract an object’s own enumerable property values.

_.values() 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