mergeWith source npm

_.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 seven arguments:
(objValue, srcValue, key, object, source, stack).

Arguments

  1. object (Object)

    The destination object.

  2. sources (...Object)

    The source objects.

  3. customizer (Function)

    The function to customize assigned values.

Returns (Object)

Returns object.

Example

function customizer(objValue, srcValue) {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

var object = {
  'fruits': ['apple'],
  'vegetables': ['beet']
};

var other = {
  'fruits': ['banana'],
  'vegetables': ['carrot']
};

_.mergeWith(object, other, customizer);
// => { 'fruits': ['apple', 'banana'], 'vegetables': ['beet', 'carrot'] }