When to clone first and how to avoid path collisions.
Fundamentals
What Is _.set()?
_.set() is Lodash’s nested write helper. You pass an object, a path, and a value; Lodash walks (or creates) the path and assigns the value at the end. It is the write counterpart to _.get() and pairs naturally with _.result() for read/invoke operations.
💡
Unlike _.omit() or _.pick()
_.set()mutates the object you pass in. If you need an immutable update, clone first or build on a fresh object.
Use it for user profiles, configuration trees, form state patches, and any time you would otherwise write fragile chains like obj.a = obj.a || {}; obj.a.b = value.
Foundation
📝 Syntax
The signature is three arguments—object, path, and value:
javascript
_.set(object, path, value)
Syntax Rules
object — the object to modify (mutated in place).
path — dot string ("profile.name"), array (["profile", "name"]), or bracket form ("users[0].name").
value — any JavaScript value to assign at the resolved location.
Return value — the same object reference (for chaining).
Missing segments — Lodash creates plain objects or array slots as needed.
javascript
import set from "lodash/set";
const user = {};
set(user, "profile.name", "John Doe");
set(user, ["profile", "age"], 30);
// user -> { profile: { name: "John Doe", age: 30 } }
The value assigned at the final segment. Replaces any existing value at that path.
_.set(user, "age", 31)
auto-createBehavior
Missing intermediate keys become plain objects. Numeric segments create or extend arrays.
_.set({}, "a.b.c", 1)
// { a: { b: { c: 1 } } }
If an intermediate value exists but is a primitive (string, number, boolean), Lodash may overwrite it to continue the path—verify structure before setting deep paths on existing data.
Hands-On
Examples Gallery
Practical _.set() patterns with sample output and interactive Try It Yourself labs.
📚 Getting Started
Build nested objects from scratch with dot paths and key arrays.
Example 1 — Create a nested profile
Start from an empty object and set profile.name and profile.age.
Lodash normalizes the path string or key array into segments.
Input
2
Walk or create
For each segment except the last, ensure an object or array slot exists.
Build
3
Assign value
Set the final segment to value, replacing any previous value there.
Write
=
✎️
Object updated
The same object reference is returned, now containing the nested value.
Important
📝 Notes
_.set()mutates the target object—clone first when immutability matters.
Missing intermediate keys are created automatically as plain objects (or array slots).
Setting a path overwrites the previous value at the final key.
Primitives along the path may be replaced if Lodash needs to continue deeper.
Use the same path formats as _.get() for consistency.
For custom intermediate objects (Maps, special merge rules), use _.setWith().
Wrap Up
Conclusion
_.set() removes the tedium of building nested object trees by hand. Pass a path and value, and Lodash creates whatever structure is needed along the way. Pair it with _.get() for safe reads and remember that it mutates in place.
When you need control over how intermediate containers are created, continue to _.setWith(). To remove nested properties instead, see _.unset() in the Lodash object series.
Clone before _.set() when immutability is required
Match path style with _.get() across your codebase
Return the object from _.set() for convenient chaining
❌ Don’t
Assume _.set() is non-mutating like _.pick()
Blindly set deep paths on API data you do not control
Overwrite primitives that should remain leaf values
Rely on sparse array behavior without testing edge indices
Forget that the final assignment replaces existing values
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.set()
Use these when writing nested data in JavaScript objects.
5
Core concepts
✎️01
Nested write
Path + value.
Basics
⚠️02
Mutates
In-place update.
Important
🚀03
Auto-create
Builds missing path.
Power
🗃️04
Arrays
[0] indices.
Pattern
🔀05
vs get
Write / read pair.
Related
❓ Frequently Asked Questions
_.set() writes a value at a nested path on an object. It mutates the object, creates missing intermediate objects or array slots along the path, and returns the same object reference.
Yes. _.set() always modifies the object you pass in. To avoid mutation, clone first: _.set(_.cloneDeep(obj), path, value) or _.set({}, path, value) when building fresh data.
A dot-path string like profile.name, bracket notation like users[0].name, or an array of keys like ["profile", "age"]. All forms reach the same nested location.
Lodash creates plain objects (or array slots for numeric indices) along the path automatically. You do not need to initialize empty nested objects manually.
_.get() reads at a path; _.set() writes at a path. They use the same path syntax but opposite directions—result is the read+invoke variant of get.
If an intermediate segment exists but is not an object (for example a string or number), _.set() may replace it to continue building the path. Plan paths carefully on existing data.
Did you know?
_.set returns the object you passed in, so you can chain writes: _.set(_.set({}, "a", 1), "b", 2). For immutable updates in UI state libraries, clone first with _.cloneDeep or build on a fresh object.