Last active 1752983411

Avoiding Nested Loops with `new Map()`

avoidingLoopsWithMap.js Raw
1let myHugeArray = [...Array(10000).keys()].map(id => { return {id}; });
2let listIWant = [1010, 2020, 3030, 4040, 5050, 6060, 7070, 8080, 9090];
3
4// INSTEAD OF:
5myHugeArray.filter(obj => listIWant.includes(obj.id));
6
7// TRY:
8let mapIWant = new Map();
9listIWant.forEach(id => mapIWant.set(id));
10myHugeArray.filter(obj => mapIWant.has(obj.id));