Thursday, October 23, 2025
HomeLanguagesJavascriptLodash _.setWith() Method

Lodash _.setWith() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, lang, function, objects, numbers etc.

The _.setWith() method is similar to _.set() method except that it accepts customizer which is invoked to produce the objects of path. And if the customizer returns undefined path creation is handled by the method instead.

Syntax:

_.setWith(object, path, value, customizer)

Parameters: This method accepts four parameters as mentioned above and described below:

  • object: It is the function which modifies the object.
  • path: It sets the path of the property.
  • value: It is used to set the values.
  • customizer: It is the function to customize the assigned values.

Return Value: This method returns the object.

Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var object = {};
  
// Using the _.setWith() method
let st_elem = _.setWith(object, 
        '[0][3]', 'd', Object);
  
// Printing the output 
console.log(st_elem);


Output:

{ '0': { '3': 'd' } }

Example 2:

javascript




// Requiring the lodash library 
const _ = require("lodash"); 
       
// Original array 
var object = {};
  
// Using the _.setWith() method
let st_elem = _.setWith(object, 
    '[0][1][2]', 'a', Object);
  
// Printing the output 
console.log(st_elem);


Output:

{ '0': {'1': { '2': 'a' } } }

Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.

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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS