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])
Call
What 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.
Use. Handy for dictionary maps without Object.prototype.
Compatibility
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 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
_.setWithLodash 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.