I have this array:
const list = [{id: 5}, {id: 1}, {id: 4}];
i want to achieve this:
const result = { 5: {id:5}, 1: {id: 1}, 4: {id: 4}};
I tried with reduce, object assign, and it always sorts by id for example:
const listById = list => {
return list.reduce((acc, cur) => {
const { id } = cur;
return {
...acc,
[id]: cur,
};
}, {});
};
This sorts by id:
{ 1: {id: 1}, 4: {id:4}, 5: {id:5}}
but I don't want that.