如何将对象数组与对象数组一起转换为对象数组

时间:2018-03-16 00:36:29

标签: javascript arrays

我有这个数组:

var test = [{
    t1: {a:1, b:2, ...},
    t2: {a:3, b:4, ...},
...,
},{
    t3: {a:5, b:6, ...},
    t4: {a:7, b:8, ...},
...,
}]

我希望它看起来像这样:

var test = [{a:1, b:2, ...},{a:3, b:4, ...},{a:5, b:6, ...},{a:7, b:8, ...}, ...]

1 个答案:

答案 0 :(得分:0)

您可以使用函数reduce,然后连接每个对象的值。



var test = [{    t1: {a:1, b:2},    t2: {a:3, b:4},},{    t3: {a:5, b:6},    t4: {a:7, b:8}}],
    result = test.reduce((a, c) => a.concat(Object.values(c)), []);

console.log(result)

.as-console-wrapper { max-height: 100% !important; top: 0; }