Lodash教程

Lodash mergeWith method

语法

_.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).

参数

object (Object) − The destination object. sources (...Object) − The source objects. customizer (Function) − The function to customize assigned values.

输出

(Object) − Returns object.

实例

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

输出

{ a: [ { b: 2 }, { d: 4 }, { c: 3 }, { e: 5 } ] }
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4