Suppose your task is removed multiple items from an array using an array:
My Working procedure:
- At first remove Items Array make it object
- Then filter array with conditionally where check is it available into that object or not.
- If not available return items
*Example: *
// array which holds all values const array = [2, 4, 6, 3]; // array of values that needs to be deleted const itemsToDeleteArr = [4, 6]; // make an object to hold values from itemsToDeleteArr const itemsToDeleteSet = new Set(itemsToDeleteArr); // use filter() method // to filter only those elements // that need not to be deleted from the array const newArr = namesArr.filter((item) => { // return those elements not in the namesToDeleteSet return !itemsToDeleteSet.has(item); }); console.log(newArr); // [2, 3]
Top comments (0)