Lodash _.update() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Object utilities

What You’ll Learn

By the end of this tutorial, you’ll use _.update() to transform nested values with an updater function at any path depth.

01

Core syntax

_.update(object, path, updater)

02

Updater fn

Receives current value; return the new one.

03

Mutates object

Modifies in place; returns same reference.

04

Path formats

Dot strings, key arrays, bracket notation.

05

update vs set

Function transform vs direct assign.

06

Common patterns

Toggle, increment, uppercase, filter.

What Is _.update()?

_.update() is Lodash’s nested transform-and-write helper. You pass an object, a path, and an updater function; Lodash reads the current value at that path, calls your function with it, and assigns the return value back. It is like _.set(), but the new value is computed from the old one.

💡
When a function beats a literal

Use _.update() for toggles (v => !v), increments (n => n + 1), string transforms, and any write that depends on the existing value.

Use it for config toggles, nested counters, normalizing user input, and patching state trees. When you already know the final value, _.set() is simpler; when you need to derive it, reach for _.update().

📝 Syntax

The signature is three arguments—object, path, and updater:

javascript
_.update(object, path, updater)

Syntax Rules

  • object — the object to modify (mutated in place).
  • path — dot string ("address.city"), array (["profile", "score"]), or bracket form ("users[0].name").
  • updater(currentValue) => newValue. Receives the value at the path (or undefined if missing).
  • Return value — the same object reference (for chaining).
  • Missing paths — like _.set(), Lodash can create intermediate objects when assigning the updater’s result.
javascript
import update from "lodash/update";

const user = {
  name: "John",
  address: { city: "New York", country: "USA" }
};

update(user, "address.city", (city) => city.toUpperCase());

// user.address.city -> "NEW YORK"

⚡ Quick Reference

TaskCode patternResult
Transform string_.update(obj, "city", v => v.toUpperCase())Uppercase nested value
Toggle boolean_.update(obj, "darkMode", v => !v)Flip true/false
Increment number_.update(obj, "count", n => (n || 0) + 1)Safe counter bump
Array path_.update(obj, "users[0].name", fn)Bracket notation
Direct assign_.set(obj, path, value)See _.set()
Custom containers_.updateWith(obj, path, fn, customizer)See _.updateWith()
Mutates?
Yes

In-place write

Updater
(v) => new

Transform fn

Returns
object

Same reference

Pair with
_.get()

Read at path

🧰 Parameters

Arguments to _.update() and how the updater transforms the value:

object Required

The target object. Lodash mutates this object directly and returns the same reference.

const state = { count: 0 };
_.update(state, "count", n => n + 1)
path Required

Location to update: string path, bracket notation, or array of keys. Same formats as _.set.

_.update(o, "address.city", fn)
_.update(o, ["profile", "score"], fn)
updater Required

Function invoked with the current value at the path. Return the new value to assign.

(city) => city.toUpperCase()
(v) => !v
return value Output

The same object reference after the path is updated (chainable like _.set()).

_.update(user, "score", s => s + 10)

Guard with _.has() when you only want to update existing paths. For immutable state, clone before calling _.update().

Examples Gallery

Practical _.update() patterns with sample output and interactive Try It Yourself labs.

📚 Getting Started

Transform nested values with a simple updater function.

Example 1 — Uppercase a nested city name

Pass the current string to an updater and write back the transformed value.

javascript
const user = {
  name: "John",
  address: { city: "New York", country: "USA" }
};

_.update(user, "address.city", (city) => city.toUpperCase());

console.log(user.address.city);
// -> "NEW YORK"
Try It Yourself

How It Works

Lodash reads "New York", your updater returns "NEW YORK", and that value is assigned back at address.city.

Example 2 — Toggle a boolean flag

Flip options.darkMode without reading and writing manually.

javascript
const config = {
  theme: "light",
  options: { darkMode: false, animation: true }
};

_.update(config, "options.darkMode", (mode) => !mode);

console.log(config.options.darkMode);
// -> true
Try It Yourself

📈 Practical Patterns

Counters, config patches, array paths, and set comparisons.

Example 3 — Increment a nested counter

Bump a page view count safely, defaulting undefined to zero first.

javascript
const stats = { page: { views: 5 } };

_.update(stats, "page.views", (n) => (n || 0) + 1);

console.log(stats.page.views);
// -> 6

Example 4 — Toggle login state

Update nested user state in one expressive call.

javascript
const state = {
  user: { name: "Alice", isLoggedIn: true }
};

_.update(state, "user.isLoggedIn", (loggedIn) => !loggedIn);

console.log(state.user.isLoggedIn);
// -> false

For immutable UI state, clone the object first—_.update() mutates in place.

Example 5 — Filter an array at a path

Replace the entire users array with a filtered copy.

javascript
const data = {
  users: [
    { id: 1, name: "John", isAdmin: false },
    { id: 2, name: "Alice", isAdmin: true },
    { id: 3, name: "Bob", isAdmin: false }
  ]
};

_.update(data, "users", (users) => users.filter((u) => u.isAdmin));

console.log(data.users);
// -> [{ id: 2, name: "Alice", isAdmin: true }]
Try It Yourself

🚀 Beyond the Basics

When update fits better than set or get+set.

Example 6 — _.update() vs _.set()

_.set() assigns a literal; _.update() derives the new value from the old one.

javascript
const item = { price: 100 };

// set: you supply the final value
_.set(item, "price", 120);

// update: compute from current value
_.update(item, "price", (p) => p * 1.1);

console.log(item.price);
// -> 132  (120 * 1.1)

🧠 How _.update() Works

1

Parse path

Lodash normalizes the path string or key array into segments.

Input
2

Read current value

The value at the path is passed to your updater (or undefined if missing).

Read
3

Assign result

The updater’s return value is written back at the path, like _.set().

Write
=

Object updated

The same object reference is returned with the transformed nested value.

📝 Notes

  • _.update() mutates the target object—clone first when immutability matters.
  • The updater receives the current value; return the new value to assign.
  • When the path is missing, the updater gets undefined and can seed a default.
  • Does not remove properties—use _.unset() for deletion.
  • Use the same path formats as _.set() and _.get().
  • For custom intermediate containers, use _.updateWith().

Conclusion

_.update() is the functional sibling of _.set(): instead of passing a final value, you pass a function that transforms the current one. It shines for toggles, counters, string normalization, and array replacements at nested paths.

Remember it mutates in place. When you need control over how missing paths are built, continue to _.updateWith().

💡 Best Practices

✅ Do

  • Use updaters for toggles, increments, and value-derived writes
  • Handle undefined in updaters when paths may not exist yet
  • Clone before _.update() when immutability is required
  • Match path style with _.set() and _.get() across your codebase
  • Prefer _.set() when you already know the final literal value

❌ Don’t

  • Use _.update() to delete properties—use _.unset()
  • Assume it is immutable like spread-based state updates
  • Forget that the updater must return the new value
  • Mutate the incoming value inside the updater without returning a copy when needed
  • Reach for update when a simple _.set() one-liner suffices

Key Takeaways

Knowledge Unlocked

Five things to remember about _.update()

Use these when the new value depends on the old one.

5
Core concepts
⚠️ 02

Mutates

In-place write.

Important
🔀 03

vs set

Fn vs literal.

Compare
🗃️ 04

Paths

Same as set/get.

Syntax
📈 05

Patterns

Toggle, +1.

Use case

❓ Frequently Asked Questions

_.update() gets the current value at a nested path, passes it to an updater function, and assigns the updater's return value back at that path. The object is mutated in place and the same object reference is returned.
_.set() assigns a value you already have. _.update() invokes a function on the existing value first—ideal for toggles, increments, uppercase transforms, and other derived writes.
The current value at the path. If the path does not exist yet, the updater receives undefined—similar to how _.set() can create missing paths when you assign a result.
Yes. Like _.set() and _.unset(), it modifies the object you pass in. Clone first when immutability matters: _.update(_.cloneDeep(obj), path, fn).
The same formats as _.set() and _.get(): dot strings like address.city, bracket notation like users[0].name, or key arrays like ["profile", "score"].
Use _.updateWith() when you need a customizer to control how intermediate objects are created along the path—similar to the set/setWith relationship.
Did you know?

_.update() is shorthand for “read at path, transform, write back”—the functional counterpart to _.set(). Some tutorials incorrectly describe it as immutable; like set and unset, it mutates the object you pass in.

Practice _.update() in the Live Editor

Uppercase nested strings, toggle booleans, and filter arrays at paths.

Open Try It editor →

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.

6 people found this page helpful