Flatten array with objects into 1 object

前端 未结 8 1501
情歌与酒
情歌与酒 2020-12-03 09:41

Given input:

[{ a: 1 }, { b: 2 }, { c: 3 }]

How to return:

{ a: 1, b: 2, c: 3 }

For arrays it\'s not a pr

8条回答
  •  独厮守ぢ
    2020-12-03 10:33

    Adding to the accepted answer, a running code snippet with ES6.

    let input = [{ a: 1 }, { b: 2 }, { c: 3 }]
    //Get input object list with spread operator
    console.log(...input)
    //Get all elements in one object
    console.log(Object.assign(...input))

提交回复
热议问题