I'm trying to remove elements in an array using a list of IDs to remove.
const dataArray = [];
const idsToRemove = new Set();
for (let i=0; i<10; i++) {
dataArray.push({id: "myID" + i, value: i});
}
for (let i=0; i<10; i+=2) {
idsToRemove.add("myID" + i);
}
const newArray = dataArray.filter(obj => !idsToRemove.has(obj.id));
console.log(newArray);
Result:
{id:"myID1", value:1}, {id:"myID3",value:3}, {id:"myID5", value:5}, {id:"myID7", value:7}, {id:"myID9",value:9}
Is this an ideal solution? Or would there be a more performant solution than this?
ADDED: Would there be a way to avoid making a newArray but just directly remove elements from dataArray?
Mapinstead ofSetto storeidsToRemoveso that it's time complexity would improve...