运算扩展符
let obj1 = {
a:'111'}
let obj2 = {
b : '222'}
let newObj = {
...obj1,...obj2}
console.log(newObj === obj1) // false
console.log(newObj === obj2) // false
//
Object.assign
let obj1 = {
a:'111'}
let obj2 = {
b : '222',a:'foo'}
let newObj = Object.assign(obj1,obj2)
console.log(newObj === obj1) // true
console.log(newObj === obj2) // false
// 后面的对象属性,会覆盖前面的对象属性
_.merge(object, [sources]) -----------工具库lodash
https://www.lodashjs.com/docs/lodash.merge#_mergeobject-sources
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 }] }