Lodash _.set() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Write a value at a nested path. Missing segments are created (arrays for index keys, objects otherwise). Part of Lodash Object (stable — not deprecated). Opposite read helper: _.get.

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

Syntax

Call _.set. Start with this line.

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

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

Library declarations
Lodash
_.set(object, path, value)

Parameters

ParameterDescription
objectRequired. Target object (mutated)
pathRequired. Property path to write
valueRequired. Value stored at the path

Return Value

Return ValueDescription
*The same mutated object

Lodash Examples

Example 1 — Official: Update an Existing Path

Overwrite a nested property that already exists.

Lodash
var object = { a: [{ b: { c: 3 } }] };

_.set(object, "a[0].b.c", 4);

console.log(object.a[0].b.c);
// → 4
Try It Yourself

How It Works

  1. Path. Walks a → [0] → b → c.
  2. Exists. Every segment is already present.
  3. Assign. Replaces 3 with 4.
  4. Docs. Matches the official first example.

Example 2 — Official: Create a Path with an Array

Missing segments are built automatically.

Lodash
var object = { a: [{ b: { c: 3 } }] };

_.set(object, ["x", "0", "y", "z"], 5);

console.log(object.x[0].y.z);
// → 5
Try It Yourself

How It Works

  1. New keys. x did not exist on object.
  2. Index. "0" creates an array slot.
  3. Objects. y then z are nested objects/keys.
  4. Docs. Matches the official second example.

Example 3 — Dot Path Creates Objects

String paths work the same way for nested objects.

Lodash
var user = {};

_.set(user, "profile.name", "Ada");

console.log(user);
// → { profile: { name: "Ada" } }
Try It Yourself

How It Works

  1. Empty. Starts from {}.
  2. Create. Builds profile as an object.
  3. Assign. Sets name to "Ada".
  4. Use. Handy for config trees and form state.

Example 4 — Returns the Same Object

The return value is the mutated source — useful for chaining.

Lodash
var object = { a: 1 };

var same = _.set(object, "b", 2);

console.log(same === object); // true
console.log(object);          // { a: 1, b: 2 }
Try It Yourself

How It Works

  1. Mutate. b is added on object.
  2. Return. Lodash returns that same reference.
  3. Identity. same === object is true.
  4. Tip. Clone before set if callers must keep the old value.

Example 5 — Pair with _.get

Write with set, read with get.

Lodash
var object = {};

_.set(object, "a[0].b.c", 4);

console.log(_.get(object, "a[0].b.c"));
// → 4
Try It Yourself

How It Works

  1. set. Builds the nested structure and stores 4.
  2. get. Reads the same path safely.
  3. Pair. Common read/write duo for deep objects.
  4. Custom. Need creation control? Switch to _.setWith.

Browser & Runtime Support

_.set 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. Native equivalents walk path segments and assign, creating {} / [] along the way.

✓ Stable · Lodash 4.x

_.set()

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

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import set from "lodash/set" for smaller builds. Use setWith for custom creation; get/result for reads.

Frequently Asked Questions

_.set(object, path, value) writes value at path. Missing path segments are created — arrays for index keys, objects otherwise. Official: _.set(object, 'a[0].b.c', 4) updates the nested c.
Yes. Official docs note that this method mutates object. Clone first if you need immutability.
It returns the same object you passed in (after mutation), which allows chaining like _.set(obj, 'a', 1).a.
Use a string ('a[0].b.c' or 'a.b.c') or an array of keys (['x', '0', 'y', 'z']). Both forms work.
Use _.setWith when you need a customizer to control how missing path segments are created (object vs array).
_.get reads a path without changing the object. _.set writes a path and may create intermediates.

Next: _.setWith()

Customize how missing path segments are created.

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