Saturday, September 6, 2025
HomeLanguagesJavascriptLodash _.extendWith() Method

Lodash _.extendWith() Method

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

The _.extendWith() method of Object in lodash is similar to _.assignIn() method and the only difference is that it accepts customizer which is called in order to generate assigned value. Moreover, if the customizer used here returns undefined then the assignment is dealt by the method instead.

Note:

  • The customizer used here can be called with five arguments namely objValue, srcValue, key, object, and source.
  • The object used here is altered by this method.

Syntax:

_.extendWith(object, sources, [customizer])

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

  • object: It is the destination object.
  • sources: It is the source objects.
  • customizer: It is the function that customizes assigned values.

Return Value: This method returns an object.

Example 1:




// Requiring lodash library
const _ = require('lodash');
  
// Defining a function customizer
function customizer(objectVal, sourceVal) {
  return _.isUndefined(objectVal) ? 
        sourceVal : objectVal;
}
  
// Calling extendWith method with its parameter
let obj = _.extendWith({ 'gfg': 1 }, 
        { 'geek': 3 }, customizer);
  
// Displays output
console.log(obj);


Output:

{ gfg: 1, geek: 3 }

Example 2:




// Requiring lodash library
const _ = require('lodash');
  
// Defining a function customizer
function customizer(objectVal, sourceVal) {
  return _.isUndefined(objectVal) ? 
        sourceVal : objectVal;
}
  
// Defining a function GfG
function GfG() {
  this.p = 7;
}
  
// Defining a function Portal
function Portal() {
  this.r = 9;
}
  
// Defining prototype of above functions
GfG.prototype.q = 8;
Portal.prototype.s = 10;
  
// Calling extendWith method
// with its parameter
let obj = _.extendWith({ 'p': 6 }, 
    new GfG, new Portal, customizer);
  
// Displays output
console.log(obj);


Output:

{ p: 6, q: 8, r: 9, s: 10 }

Reference: https://lodash.com/docs/4.17.15#assignInWith

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
32269 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS