Question: I currently have the following data structure:

```
const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

```

And I need to turn it into this:

```
const foo = {
    1:{id:1, version:0, name:"test name A"},
    2:{id:2, version:0, name:"test name B"},
    3:{id:3, version:0, name:"test name C"}
};

```

The piece of code I actually have is this:

```
for(let i=0;len = bar.length; i< len;i++){
    foo[bar[i].id]= bar[i];
}

```

I've tried doing

```
bar.map((element,index)=>{
    const temporal = {[index]:element};
    foo = {...foo, temporal};
});

```

but I'm lost, any suggestions?

Answer: 
You can use `reduce()` with `Object.assign()`

```js
const bar = [
    {id:1, version:0, name:"test name A"},
    {id:2, version:0, name:"test name B"},
    {id:3, version:0, name:"test name C"}
];

var result = bar.reduce((r, e) => Object.assign(r, {[e.id]: e}), {});
console.log(result)
```
You could use [`Object.assign`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) with [`Array#map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and [spread syntax `...`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator)

```js
const
    bar = [{ id: 1, version: 0, name: "test name A" }, { id: 2, version: 0, name: "test name B" }, { id: 3, version: 0, name: "test name C" }],
    object = Object.assign(...bar.map(o => ({ [o.id]: o })));

console.log(object);
```

```css
.as-console-wrapper { max-height: 100% !important; top: 0; }
```