Convert Javascript array of objects into one object

前端 未结 4 2154
别跟我提以往
别跟我提以往 2020-12-20 06:30

Let\'s say I have an array like this

var myarray=[{\"id\":1234, \"listtext\":open, \"prop3\":value3 ,\"prop4\": value4},
             {\"id\":1235, \"listtex         


        
4条回答
  •  忘掉有多难
    2020-12-20 07:06

    If you are using ES6 or above:

    const converted = Object.assign({}, ...myArray.map(object => ({[object.id]: object})))
    

提交回复
热议问题