Lodash - mergeWith method



Syntax

_.mergeWith(object, sources, customizer)

This method is like _.merge except that it accepts customizer which is invoked to produce the merged values of the destination and source properties. If customizer returns undefined, merging is handled by the method instead. The customizer is invoked with six arguments:(objValue, srcValue, key, object, source, stack).

Arguments

  • object (Object) − The destination object.

  • sources (...Object) − The source objects.

  • customizer (Function) − The function to customize assigned values.

Output

  • (Object) − Returns object.

Example

var _ = require('lodash');
var object = {
   'a': [{ 'b': 2 }, { 'd': 4 }]
}; 
var other = {
   'a': [{ 'c': 3 }, { 'e': 5 }]
};
function customizer(objValue, srcValue) {
   if (_.isArray(objValue)) {
     return objValue.concat(srcValue);
   }
}

console.log(_.mergeWith(object, other, customizer));

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

{ a: [ { b: 2 }, { d: 4 }, { c: 3 }, { e: 5 } ] }
lodash_object.htm
Advertisements