将多个对象合并到多个数组js中

时间:2018-09-20 09:56:11

标签: javascript arrays flatten

我希望将所有对象合并为一个大数组。 例如 我将多个对象数组组合成一个大数组

    [ 
      [ { slot: 0, time: '08:00' }, { slot: 1, time: '08:10' } ],
      [ { slot: 2, time: '08:20' }, { slot: 3, time: '08:30' } ],
      [ { slot: 4, time: '08:40' }, { slot: 5, time: '08:50' } ],
      [ { slot: 6, time: '09:00' }, { slot: 7, time: '09:10' } ],
      [ { slot: 8, time: '09:20' }, { slot: 9, time: '09:30' } ]
    ]

我的预期结果是

 [
    { slot: 0, time: '08:00' },
    { slot: 1, time: '08:10' },
    { slot: 2, time: '08:20' },
    { slot: 3, time: '08:30' },
    { slot: 4, time: '08:40' },
    { slot: 5, time: '08:50' },
    { slot: 6, time: '09:00' },
    { slot: 7, time: '09:10' },
    { slot: 8, time: '09:20' },
    { slot: 9, time: '09:30' }
 ]

1 个答案:

答案 0 :(得分:1)

您可以将Array.concatspread syntax结合使用。

var data = [ [ { slot: 0, time: '08:00' }, { slot: 1, time: '08:10' } ], [ { slot: 2, time: '08:20' }, { slot: 3, time: '08:30' } ], [ { slot: 4, time: '08:40' }, { slot: 5, time: '08:50' } ], [ { slot: 6, time: '09:00' }, { slot: 7, time: '09:10' } ], [ { slot: 8, time: '09:20' }, { slot: 9, time: '09:30' } ] ],
    result = [].concat(...data);
console.log(result);