【发布时间】:2019-10-02 05:51:28
【问题描述】:
如何使用 ES6 扩展运算符合并数组中的对象?假设我们有这样的对象:
let arr = [{ a: 1, b: true }, { c: "val", d: null }];
结果是这个对象:
{ a: 1, b: true, c: "val", d: null };
【问题讨论】:
如何使用 ES6 扩展运算符合并数组中的对象?假设我们有这样的对象:
let arr = [{ a: 1, b: true }, { c: "val", d: null }];
结果是这个对象:
{ a: 1, b: true, c: "val", d: null };
【问题讨论】:
您可以将元素分散到Object.assign 中,而无需进一步循环。
let arr = [{ a: 1, b: true }, { c: "val", d: null }],
result = Object.assign({}, ...arr);
console.log(result);
【讨论】:
【讨论】:
Object.assign({}, ...arr)