0

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);
1
  • Please indent/format your code. Commented Jan 23, 2016 at 4:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Or you could use includes, which would also find NaN :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.