You want to reduce your array into a map:
const arr = [{id:1},{id:2},{id:2}];
const map = arr.reduce((acc, item) => acc.set(item.id, item), new Map());
console.log(map.get(1));
Here is a JSPref against using map
and forEach
.
In Chrome v53 reduce
is fastest, then forEach
with map
being the slowest.