0

I need to filter an array1, with each element in my array 2. Both arrays can have a random number of elements.

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

 array2.forEach(function(x){
   filteredArray = array1.filter(function(y){
     return y !== x;
   });
 });
 return filteredArray;
 //should return -> [2,4]
 // returns [1,2,3,4]

How can I filter an array with all elements in another array?

1

6 Answers 6

2

use arrays indexOf method

var array1 = [1,2,3,4,5];
var array2 = [1,3,5];
var filteredArray = array1.filter(function(x) {
    return array2.indexOf(x) < 0;
});

or, for sexy people, use !~ with indexOf

var array1 = [1,2,3,4,5];
var array2 = [1,3,5];
var filteredArray = array1.filter(function(x) {
    return !~array2.indexOf(x);
});
Sign up to request clarification or add additional context in comments.

7 Comments

!array2.includes(x) (ECMAScript 2016)
yes, with ecmascript 2016 ... no browser is 100% ecmascript 2015 yet, so, hell, let's just jump to ecmascript 2020 :p
It's just a method. Patching is easy, even without a transpiler.
Thank you so much!
in fact, array#filter wont work in IE8 or less ... so all those ATM machines would crash with this code :p
|
1

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

filteredArray = array1.filter(function (y) {
		return array2.indexOf(y) < 0;
	});
console.log(filteredArray);

Comments

1

A much simpler way would be:

var filteredArray = array1.filter(function(item) {
    return !(array2.indexOf(item) >= 0);
});

6 Comments

that wont work because 0...n should result in false, and -1 in true
Still doesn't work. If you put in return !(array2.indexOf(item) >= 0); that should do the trick.
sexy people use !~
isn't !( ... >= 0) just ... < 0
yes, as will ! >= 0 ... 0 >= 0 is true, !true is false ... trust me ... !(a.indexOf(b) >= 0) is 100% identical to a.indexOf(b) < 0 ... or, more simply ... !(a >= 0) === a < 0
|
0

You can use indexOf() to check if the array2 item is in array1 then only add it to filteredArray if it is:

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

 array2.forEach(function(x){
     if (array1.indexOf(array2[x] > -1) {
         filteredArray.push(array2[x]);
     }
 });
 return filteredArray;

Comments

0

In ES6, you could use Set for it.

var array1 = [1, 2, 3, 4, 5],
    array2 = [1, 3, 5],
    filteredArray = array1.filter((set => a => !set.has(a))(new Set(array2)));

console.log(filteredArray);

2 Comments

We can make it even more convoluted..! How about filteredArray = array1.filter(function(set,a) {return !set.has(a)}.bind(null,new Set(array2)));..?
in this case, you could use new Set(...) as this args.
0

All indexOf no play makes me a dull boy...

var array1 = [1,2,3,4,5],
    array2 = [1,3,5],
  filtered = array1.filter(e => !array2.includes(e));
console.log(filtered);

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.