Ad

How To Filter An Array And Compare It With The Function Arguments In Javascript?

- 1 answer

I have tried the below code, but I am not getting satisfactory results.

function destroyer(arr) {
// Remove all the values

var newArray = arr.filter(function(x){

if(x == arr[0]){
 return x;
}
});
return newArray;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Ad

Answer

The simplest one:

function without(array, exclude) {
    return array.filter(function(x) { return exclude.indexOf(x) === -1; });
}

You could use it like this: without([1,2,3,4,5], [1,2]) // returns [3,4,5]

Or you could try dealing with arguments list like this, but the idea would be the same.

Ad
source: stackoverflow.com
Ad