function mergeObject(...args){
// 声明一个空数组
const result = {}
// 遍历所有参数对象
args.forEach(obj=>{
// 获取当前对象的所有属性
Object.keys(obj).forEach(key=>{
// 检测result中是否含有key属性
if(result.hasOwnProperty(key)){
result[key] = [].concat(result[key],obj[key])
}else{
result[key] = obj[key]
}
})
})
return result
}
const object = {
a:[{x:2},{y:4}],
b:1
}
const other = {
a:{ z:3 },
b:[2,3],
c:'foo'
}
let result = mergeObject(object,other)
console.log(result)
{"a": [{"x": 2},{"y": 4},{"z": 3}],"b": [1,2,3],"c": "foo"}