convert array of objects into object of objects properties from array

前端 未结 4 1850
名媛妹妹
名媛妹妹 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:09

    You could spread the array as parameters (spread syntax ...) for Object.assign, which returns a single object.

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

提交回复
热议问题