Thursday, September 4, 2025
HomeLanguagesJavascriptLodash _.update() Method

Lodash _.update() Method

Lodash _.update() method accepts an updater to produce the value to set. This method uses _.updateWith() function to customize path creation. It is almost the same as the _.set() function.

Syntax:

_.update(object, path, updater);

Parameters:

  • object: This parameter holds the object to modify.
  • path: This parameter holds the path of the property to set. It will be an array or string.
  • updater: This is the function to produce the updated value.

Return Value:

This method returns the new object. 

Example 1: In this example, we are updating the value of an object by using the _.update() method.

Javascript




// Requiring the lodash library
const _ = require("lodash");
 
// The source object
let obj = { cpp: [{ java: { python: 3 } }] };
 
// Use of _.update() method
_.update(obj, "cpp[0].java.python", function (n) {
    return n * n;
});
 
// return the new object
console.log(obj.cpp[0].java.python);


Output:

9

Example 2: In this example, we are updating the value of an object which is not present in that object and according the function which is passed in the _.update() method it is returning 0.

Javascript




// Requiring the lodash library 
const _ = require("lodash");
 
// The source object
let obj = { 'cpp': [{ 'java': { 'python': 3 } }] };
 
// Use of _.update() method
_.update(obj, 'html[0].css.javascript',
    function (n) { return n ? n + 1 : 0; });
 
// return the new object
console.log(obj.html[0].css.javascript);


Output:

0
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS