简单的通过循环对象的 key , 如果 key 还是一个对象 通过递归来不断的进行对象的拷贝。
export function deepMerge(...objs: any[]): any {
const result = Object.create(null)
objs.forEach(obj => {
if (obj) {
Object.keys(obj).forEach(key => {
const val = obj[key]
if (isPlainObject(val)) {
// 递归
if (isPlainObject(result[key])) {
result[key] = deepMerge(result[key], val)
} else {
result[key] = deepMerge(val)
}
} else {
result[key] = val
}
})
}
})
return result
}
export function isPlainObject(val: any): val is Object {
return toString.call(val) === '[object Object]'
}