convert array of objects into object of objects properties from array

前端 未结 4 1892
名媛妹妹
名媛妹妹 2021-01-19 13:01

I need to convert an array of objects into an object of objects properties from the array.

Here is an example of an array of objects

co         


        
4条回答
  •  情书的邮戳
    2021-01-19 13:14

    You can use .reduce() and Object.assign() methods:

    const array = [
      {book:5, car: 6, pc: 7},
      {headphone: 9, keyboard: 10},
    ];
    
    const result = array.reduce((r, c) => Object.assign(r, c), {});
    
    console.log(result);

提交回复
热议问题