Lodash _.setWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Like _.set, but a customizer can produce each missing path container. If it returns undefined, Lodash creates arrays for indexes and objects otherwise. Part of Lodash Object (stable — not deprecated).

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

Syntax

Call _.setWith. Start with this line.

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

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

Library declarations
Lodash
_.setWith(object, path, value, [customizer])

Parameters

ParameterDescription
objectRequired. Target object (mutated)
pathRequired. Property path to write
valueRequired. Leaf value at the path
customizerOptional. Builds missing intermediates; undefined → default

Return Value

Return ValueDescription
*The same mutated object

Lodash Examples

Example 1 — Official: Force Objects for Indexes

Pass Object so numeric keys stay plain objects.

Lodash
var object = {};

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

How It Works

  1. Path. [0][1] looks like nested indexes.
  2. Customizer. Object builds {} instead of [].
  3. Leaf. Final value 'a' is assigned normally.
  4. Docs. Matches the official Lodash example.

Example 2 — setWith vs set

Default _.set creates arrays for index paths.

Lodash
console.log(_.setWith({}, "[0][1]", "a", Object));
// → { "0": { "1": "a" } }

console.log(_.set({}, "[0][1]", "a"));
// → [ [ undefined, "a" ] ]
Try It Yourself

How It Works

  1. setWith. Object → nested plain objects.
  2. set. Indexes → nested arrays.
  3. Same leaf. Both store 'a' at the end.
  4. Pick. Use setWith when object-shaped keys matter.

Example 3 — Customizer Returns undefined

Fall back to Lodash’s default container rules.

Lodash
var object = {};

_.setWith(object, "a.b", 1, function () {
  return undefined;
});
// → { a: { b: 1 } }
Try It Yourself

How It Works

  1. Call. Customizer runs for missing a.
  2. undefined. Signals “use the default.”
  3. Default. Non-index key → plain object.
  4. Leaf. b is set to 1.

Example 4 — Force Arrays with Array

Pass Array when you want array containers explicitly.

Lodash
var object = {};

_.setWith(object, "0.0", "x", Array);

console.log(Array.isArray(object[0])); // true
console.log(object[0][0]);             // "x"
Try It Yourself

How It Works

  1. Customizer. Array builds [] for missing keys.
  2. Nest. Outer and next segment become arrays.
  3. Leaf. Stores "x" at [0][0].
  4. Note. Constructors work because Lodash calls them like factories.

Example 5 — Custom Function Factory

Return your own container from a function customizer.

Lodash
var object = {};

_.setWith(object, "meta.tag", "ok", function () {
  return Object.create(null);
});

console.log(object.meta.tag);           // "ok"
console.log(Object.getPrototypeOf(object.meta) === null); // true
Try It Yourself

How It Works

  1. Factory. Customizer returns Object.create(null).
  2. Prototype. Intermediate has no inherited keys.
  3. Leaf. tag is still set to "ok".
  4. Use. Handy for dictionary maps without Object.prototype.

Browser & Runtime Support

_.setWith is a Lodash Object helper available in 4.x. It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash. Prefer it over _.set only when you must customize intermediate creation.

✓ Stable · Lodash 4.x

_.setWith()

Available in [email protected] full builds and the lodash/setWith 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
_.setWith Lodash 4.x

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import setWith from "lodash/setWith" for smaller builds. Use set for default rules; updateWith when the leaf needs a transform.

Frequently Asked Questions

_.setWith(object, path, value, [customizer]) is like _.set, but the customizer can produce each missing path container. Official: _.setWith({}, '[0][1]', 'a', Object) → { '0': { '1': 'a' } }.
It is invoked as customizer(nsValue, key, nsObject) when a path segment needs creating. Return a new container, or undefined to let Lodash choose [] or {}.
Yes. Like _.set, it mutates object and returns the same reference.
_.set always creates arrays for index keys and objects otherwise. _.setWith lets you override that — e.g. pass Object so '[0][1]' becomes nested plain objects.
No. It only builds intermediate containers along the path. The leaf value is still the value argument.
Use _.updateWith when you need both a customizer for path creation and an updater function for the existing leaf value.

Next: _.toPairs()

Turn an object into an array of [key, value] pairs.

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