Skip to content

mergeWith

js
_.mergeWith(object, sources, customizer)

此方法与 _.merge 类似,不同之处在于它接受 customizer,调用 customizer 可生成目标属性和源属性的合并值。如果 customizer 返回 undefined,则合并将由该方法处理。customizer 使用六个参数调用:(objValue, srcValue, key, object, source, stack)。

¥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

¥Note: This method mutates object.

新增于

¥Since

4.0.0

参数

¥Arguments

  1. object (对象):目标对象。

    ¥object (Object): The destination object.

  2. sources (...对象):起始位置。

    ¥sources (...Object): The source objects.

  3. customizer (函数):自定义分配值的函数。

    ¥customizer (Function): The function to customize assigned values.

返回

¥Returns

(对象):返回 object

¥(Object): Returns object.

示例

¥Example

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

var object = { 'a': [1], 'b': [2] };
var other = { 'a': [3], 'b': [4] };

_.mergeWith(object, other, customizer);
// => { 'a': [1, 3], 'b': [2, 4] }

Lodash v4.17 中文网 - 粤ICP备13048890号