1

I am trying to use the filter method on an array to loop through the array based on a variable number of arguments.

Below is my attempt at this:

function destroyer(arr) {
  var argArr = arr.slice.call(arguments, 1);
  var filteredArray = arr.filter(function(val) {
    for (var i = 0; i < argArr.length; i++) {
        return val != argArr[i];
    };
  });
  console.log(filteredArray);
}

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

When I do this, only the first element of the arguments array is disposed of. This therefore returns:

[1, 3, 1, 3]

I have found a few examples online of possible ways to resolve this but they are vastly different from what I understand just yet. Is there any way to get mine to work, or even understand why the additional elements of the arguments array are not being called.

1
  • 1
    The "problem" you're experiencing is caused by the return statement effectively limiting the loop to one iteration. Commented Jul 20, 2017 at 10:38

3 Answers 3

3

If you use ES6 you can do it with rest operator and Array#includes function

function destroyer(arr, ...params){
    return arr.filter(item => !params.includes(item));
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

With your logic you can do like this. If val is equal to the current argArr's item then return false, if nothing was found after the loop: return true

function destroyer(arr) {
  var argArr = Array.prototype.slice.call(arguments, 1);
  var filteredArray = arr.filter(function(val) {
    for (var i = 0; i < argArr.length; i++) {
         if(val === argArr[i]){
             return false;
         }
    };
    return true;
  });
  console.log(filteredArray);
}

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

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

1 Comment

Thanks for this, exactly what I was looking to do!
0

Because with your code you always test if current element in filter is equal or not equal to second parameter in function which is 2 and return true/false. Instead you can use indexOf to test if current element in filter is inside arguments array.

function destroyer(arr) {
  var argArr = arr.slice.call(arguments, 1);
  var filteredArray = arr.filter(function(val) {
    return argArr.indexOf(val) == -1
  });
  console.log(filteredArray);
}

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

Comments

0

Problem is in your this line return val != argArr[i];

Change logic like this , it will avoid to do extra looping also .

function destroyer(arr) {
  var argArr = arr.slice.call(arguments, 1); debugger
  var filteredArray = arr.filter(function(val) {
    return !(argArr.indexOf(val) >= 0);
  });
  console.log(filteredArray);
}

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

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.