是否可以将嵌套对象添加在一起。我知道Object.assign可以合并两个对象。例如:
const target = { a: 1, b: { c: 2, d: 3 } };
const source = { e: 7, b: { c: 1, d: 5 } };
const returnedTarget = Object.assign(target, source);
returnedTarget = { a: 1, b: { c: 1, d: 5 }, e: 7 }
但是总有没有添加变量C和D而不是用源代码覆盖它们吗?
一个实际的用例示例:
[
Character1: {
kills: { value: 10.0, displayValue: "10" },
deaths: { value: 5.0, displayValue: "5" }
},
Character2: {
kills: { value: 20.0, displayValue: "20" },
deaths: { value: 15.0, displayValue: "15" }
},
]
//Expected Result:
[
Merged: {
kills: { value: 30.0, displayValue: "30" },
deaths: { value: 20.0, displayValue: "20" }
}
]