How to create a new array by raising the id attribute in the object array

while learning js, I saw an article suggesting that arrays should be modeled, and now I have some doubts.

original array:

[{id:"111",title:"title1",body:"body1"},
{id:"222",title:"title2",body:"body2"}]

id


want to learn to use Object.assign or other methods, but no matter how to write it wrong, ask for advice

knows that writing like this can only copy the original array
Object.keys (org). Map (key = > Object.assign ({}, org.key])
but how to propose a layer?

Mar.12,2021

elegantly unexpectedly, simple and rude can use the following

const arr = [
  { id: '111', title: 'title1', body: 'body1' },
  { id: '222', title: 'title2', body: 'body2' }
]

const obj = {}

arr.forEach(item => {
  obj[item.id] = item
})

console.log(obj)

const arr = [
  { id: '111', title: 'title1', body: 'body1' },
  { id: '222', title: 'title2', body: 'body2' }
]

const obj=arr.reduce((sum,next)=>({...sum,[next.id]:next}),{})
console.log(obj)
Menu