Friday, January 16, 2026
HomeLanguagesJavascriptLodash _.mergeWith() Method

Lodash _.mergeWith() 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 _.mergeWith() method uses a customizer function that is invoked to produce the merged values of the given destination and source properties. When the customizer function returns undefined, the merging is handled by the method instead. It is almost the same as _.merge() method.

Syntax:

_.mergeWith( object, sources, customizer )

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

  • object: This parameter holds the destination object.
  • sources: This parameter holds the source object. It is an optional parameter.
  • customizer: This is the function to customize assigned values.

Return Value: This method returns the object.

Example 1:

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// The destination object
var object = {
  'amit': [{ 'susanta': 20 }, { 'durgam': 40 }]
};
   
// The source object
var other = {
  'amit': [{ 'chinmoy': 30 }, { 'kripamoy': 50 }]
};
  
// Using the _.mergeWith() method 
console.log(_.mergeWith(object, other));


Output:

{ ‘amit’: [{‘chinmoy’: 30, ‘susanta’: 20 }, { ‘durgam’: 40, ‘kripamoy’: 50 }] }

Example 2:  

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
  
// Defining the customizer function
function customizer(obj, src) {
  if (_.isArray(obj)) {
    return obj.concat(src);
  }
}
   
// The destination object
var object = {
  'amit': [{ 'susanta': 20 }, { 'durgam': 40 }]
};
   
// The source object
var other = {
  'amit': [{ 'chinmoy': 30 }, { 'kripamoy': 50 }]
};
  
// Using the _.mergeWith() method 
console.log(_.mergeWith(object, other, customizer));


Output:

{ ‘amit’: [{‘susanta’: 20 }, { ‘durgam’: 40}, {‘chinmoy’: 30}, {‘kripamoy’: 50 } ]}

RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS