主题
merge
js
_.merge(object, [sources])
此方法与 _.assign
类似,不同之处在于它以递归方式将源对象的自身和继承的可枚举字符串键属性合并到目标对象中。如果存在目标值,则跳过解析为 undefined
的源属性。数组和普通对象属性以递归方式合并。其他对象和值类型将被赋值覆盖。源对象从左到右应用。后续源将覆盖先前源的属性分配。
¥This method is like _.assign
except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined
are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.
注意:此方法改变 object
。
¥Note: This method mutates object
.
新增于
¥Since
0.5.0
参数
¥Arguments
object
(对象):目标对象。¥
object
(Object): The destination object.[sources]
(...对象):起始位置。¥
[sources]
(...Object): The source objects.
返回
¥Returns
(对象):返回 object
。
¥(Object): Returns object
.
示例
¥Example
js
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }