1

Using pure javascript, starting with an array, I would like to return and array by removing values that match any value in a second array.

I have solved this problem, but I believe with more code than is really necessary.

I am hoping for a more concise or elegant solution using only javascript.

function removeValues(arr){
    array = arguments[0];
    args = Array.prototype.slice.call(arguments);
    len = arguments.length;
        filtered = array.filter(function(n){
            x = true;
            for (var i = 1; i < len; i++) {
                if (n == args[i]) { x = false; }
            }
            return x;
        });
    return filtered;
}

removeValues([1,2,3,1,2,3],2,3);

Should use a function that removes values from the first argument (an array) using values in one or more additional arguments.

6
  • api.jquery.com/jquery.grep but its not pure JS... Commented Sep 2, 2015 at 11:37
  • 1
    using only pure javascript Commented Sep 2, 2015 at 11:37
  • Why would anyone use jQuery for this? Commented Sep 2, 2015 at 11:38
  • Its not only this, but when u are making bigger app, framework is saving lot of time Commented Sep 2, 2015 at 11:39
  • agreed. This is a JS algorithm problem, so not about saving time but practicing programming. I will read the docs on grep though, thanks for that it was helpful. Commented Sep 2, 2015 at 11:40

3 Answers 3

3

When you're working with the filter function is not necessary to use loops because you're already in a loop. After converting the arguments into an array with [].slice.call(arguments), you could use indexOf that is responsible for returning the position of a value in an array, if a value is not exists, this returns -1, so we will take all the results that are -1

Your code could be reduced as well:

function removeValues(arr){
  return arr.filter(function(val){
    return [].slice.call(removeValues.arguments).slice(1).indexOf(val) === -1
  })
}

console.log(removeValues([1,2,3,1,2,3],2,3))


ES6 Method: Using Rest parameters and Arrow Functions

enter image description here

var removeValues = (arr, ...values) => arr.filter(val => values.indexOf(val) === -1)
Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain a bit more in the answer how this works?
If you mean to ES6 method, you could go to 2 typed links, is where is explained in detail about Rest parameters and Arrow Functions, the operation is the same
3

Try this instead...

function removeValues(){
    var args = Array.prototype.slice.call(arguments).slice(1);
    return arguments[0].filter(function(value) {
        return args.indexOf(value) === -1;
    });
}

removeValues([1, 2, 3, 1, 2, 3], 2, 3);

It does the exact same thing, but tidies it slightly.

1 Comment

Array.prototype.slice.call(arguments), also take the first argument, to be exact should be: Array.prototype.slice.call(arguments).slice(1). args = in strict mode it will fail, it should be declared as local variable var args =
1

Try like this:

var array1 = [ 1, 2, 3, 4, 5 ];
var array2 = [ 2, 3 ];

var result = array1.filter( function ( elem ) {
    return array2.indexOf( elem ) === -1;
});

See example: Running code

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.