xxxxxxxxxx
myArray = myArray.filter( function( el ) {
return toRemove.indexOf( el ) < 0;
} );
xxxxxxxxxx
myArray = myArray.filter( ( el ) => !toRemove.includes( el ) );
xxxxxxxxxx
myArray = myArray.filter( function( el ) {
return !toRemove.includes( el );
} );
xxxxxxxxxx
// array which holds all values
const namesArr = ["Lily", "Roy", "John", "Jessica"];
// array of values that needs to be deleted
const namesToDeleteArr = ["Roy", "John"];
// make a Set to hold values from namesToDeleteArr
const namesToDeleteSet = new Set(namesToDeleteArr);
// use filter() method
// to filter only those elements
// that need not to be deleted from the array
const newArr = namesArr.filter((name) => {
// return those elements not in the namesToDeleteSet
return !namesToDeleteSet.has(name);
});
console.log(newArr); // ["Lily", "Jessica"]