I want to delete the numbers in an array from the original array if each element in compared using ES6 standards
const numbers = [1,4,2,3,54];
const output = except(numbers,[1,54]);
Where except is a function that expects an array (original one) and the numbers user wants to remove
This is how i achieved this, obviously there are better implementations or simpler ones
function except(array,excluded) {
console.log(array.filter((item) => excluded.indexOf(item) === -1));
}
the function except expects the original array and the values user wants to remove, according to MDN filter method creates a new array with all elements that pass the test implemented by the provided function.
In our case the provided function is the indexOf(item) , where -1 means if not equal to, so the filter function is filtering values after removing those that are in excluded array. So the output we see is:
Array(3) [ 4, 2, 3 ]