如何将二维数组转换为对象数组

问题描述 投票:0回答:1

我有一个看起来像这样的数组

arr = [
[{a: 1}, {b: 2}],
[{a: 5}, {b: 1}]
]

我想将该数组转换为如下所示的内容

arr = [
{ {a: 1}, {b: 2} },
{ {a: 5}, {b: 1} }
]
javascript arrays multidimensional-array
1个回答
0
投票

const arr = [
[{a: 1}, {b: 2}],
[{a: 5}, {b: 1}]
];

const result = arr.map(arr => arr.reduce((r, obj) => Object.keys(obj).forEach(key => r[key] = obj[key]) || r, {}));
console.log(result);

© www.soinside.com 2019 - 2024. All rights reserved.