How to use filter and return values not found in an array from another array

How to use filter and return values not found in an array from another array
0.0 0

#1

Tell us what’s happening:

How can I check/pass into the return statement inside the filter for the content of the array for different values, instead of passing individual values.

Your code so far

function destroyer(arr) {
  //let a = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));  // converting arguments to a real array

  // 1. ES2015 method of converting arguments to a real array
  var a = Array.from(arguments);  //

  var b = a .slice(1);

  var c = arr.filter(function(es) {

    return  es !== 2 && es !== 3;

  });
  
  return c;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36.

Link to the challenge:


#2

You declared b and assigned it an array containing 2 and 3, so use b inside your filter callback function. You could iterate through b and check if the current es is not equal to the current element in b.


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


  //let a = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments));  // converting arguments to a real array

  // 1. ES2015 method of converting arguments to a real array
  let a = Array.from(arguments);  //

  let b = a .slice(1);

  let c = arr.filter(function(es) {

    for(let elem of b) {
      return  es !== elem;
    }

  });


  return c;
}




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

this only return [1,3,1,3]
am sure the loop exit immediately it find 2 so it does not check the 3. other wise it would have return [1.1]

how do I prevent that?

thanks


#4

If you can not figure it out with the for loop, then try using indexOf. The iteration is done for you and returns an index value >=0 if found or -1 if not found.


#5

Thanks I will into that. i will let you know.


#6

Thanks I figured it out with indexOf