Find an object in an array the fast way in JavaScript
First Method
const poeple = [
{id: 1, name: 'youssef'},
{id: 2, name: 'kamal'},
{id: 3, name: 'ossama'},
{id: 4, name: 'yassir'},
];
const poepleById = poeple.reduce((byId, item) => {
byId.set(item.id, item);
return byId;
}, new Map());
console.log(poepleById.get(2)); // {id: 2, name: 'kamal'}
Second Method
`const poepleById = {};
poeple.map(item => { poepleById[item.id] = item; return item; });`
console.log(poepleById[2]); // {id: 2, name: 'kamal'}
I hope you like it!