发布于:2021-02-17 00:02:23
0
93
0
我最近分享了如何使用传播运算符合并对象属性, 但是此方法有一个很大的局限性:传播运算符合并不是“深度”合并,这意味着合并是递归的。此外,嵌套对象的属性不会合并-合并中指定的最后一个值将替换最后一个,即使应该存在其他属性也是如此。
const defaultPerson = {
name: 'Anon',
gender: 'Female',
hair: {
color: 'brown',
cut: 'long'
},
eyes: 'blue',
family: ['mom', 'dad']
};
const me = {
name: 'David Walsh',
gender: 'Male',
hair: {
cut: 'short'
},
family: ['wife', 'kids', 'dog']
};
const summary = {...defaultPerson, ...me};
/*
{
"name":"David Walsh",
"gender":"Male",
"hair":{
"cut":"short"
},
"eyes":"blue",
"family":[
"wife",
"kids",
"dog"
]
}
*/
在上面的示例中,您会注意到hair 对象的color 消失了而不是合并了,因为散布运算符只保留最后提供的值,在这种情况下为me.hair。相同的合并问题也适用于数组-您会注意到mom ,dad 而不是从defaultPerson对象的family数组中合并。
JavaScript中的深度合并很重要,尤其是在具有许多属性的“默认”或“选项”对象以及通常与实例特定值合并的嵌套对象的常规做法中。如果您正在寻找可用于深度合并的实用程序,那就比微型的deepmerge实用程序好!
使用该deepmerge 实用程序时,可以将任意数量的对象(包括数组)递归合并到一个最终对象中。让我们来看看!
const deepmerge = require('deepmerge');
// ...
const summary = deepmerge(defaultPerson, me);
/*
{
"name":"David Walsh",
"gender":"Male",
"hair":{
"color":"brown",
"cut":"short"
},
"eyes":"blue",
"family":[
"mom",
"dad",
"wife",
"kids",
"dog"
]
}
*/
deepmerge可以处理更复杂的合并:嵌套对象和deepmerge.all合并两个以上的对象:
const result = deepmerge.all([,
{ level1: { level2: { name: 'David', parts: ['head', 'shoulders'] } } },
{ level1: { level2: { face: 'meh', parts: ['knees', 'toes'] } } },
{ level1: { level2: { eyes: 'more meh', parts: ['eyes'] } } },]);/*
{
"level1":{
"level2":{
"name":"David",
"parts":[
"head",
"shoulders",
"knees",
"toes",
"eyes"
],
"face":"meh",
"eyes":"more meh"
}
}
}
*/
deepmerge 是一个了不起的实用程序,是相对较少的代码:
function isMergeableObject(val) {
var nonNullObject = val && typeof val === 'object'
return nonNullObject && Object.prototype.toString.call(val) !== '[object RegExp]'
&& Object.prototype.toString.call(val) !== '[object Date]'}function emptyTarget(val) {
return Array.isArray(val) ? [] : {}}function cloneIfNecessary(value, optionsArgument) {
var clone = optionsArgument && optionsArgument.clone === true
return (clone && isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, optionsArgument) : value}function defaultArrayMerge(target, source, optionsArgument) {
var destination = target.slice()
source.forEach(function(e, i) {
if (typeof destination[i] === 'undefined') {
destination[i] = cloneIfNecessary(e, optionsArgument)
} else if (isMergeableObject(e)) {
destination[i] = deepmerge(target[i], e, optionsArgument)
} else if (target.indexOf(e) === -1) {
destination.push(cloneIfNecessary(e, optionsArgument))
}
})
return destination}function mergeObject(target, source, optionsArgument) {
var destination = {}
if (isMergeableObject(target)) {
Object.keys(target).forEach(function (key) {
destination[key] = cloneIfNecessary(target[key], optionsArgument)
})
}
Object.keys(source).forEach(function (key) {
if (!isMergeableObject(source[key]) || !target[key]) {
destination[key] = cloneIfNecessary(source[key], optionsArgument)
} else {
destination[key] = deepmerge(target[key], source[key], optionsArgument)
}
})
return destination}function deepmerge(target, source, optionsArgument) {
var array = Array.isArray(source);
var options = optionsArgument || { arrayMerge: defaultArrayMerge }
var arrayMerge = options.arrayMerge || defaultArrayMerge if (array) {
return Array.isArray(target) ? arrayMerge(target, source, optionsArgument) : cloneIfNecessary(source, optionsArgument)
} else {
return mergeObject(target, source, optionsArgument)
}}deepmerge.all = function deepmergeAll(array, optionsArgument) {
if (!Array.isArray(array) || array.length < 2) {
throw new Error('first argument should be an array with at least two elements')
}
// we are sure there are at least 2 values, so it is safe to have no initial value
return array.reduce(function(prev, next) {
return deepmerge(prev, next, optionsArgument)
})}
功能强大的小代码?那是我最喜欢的实用程序类型! deepmerge 有充分的理由在整个网络上使用!
作者介绍