I have a target array ["apple","banana","orange"]
, and I want to check if other arrays contain any one of the target array elements.
For example:
["apple","grape"] //returns true;
["apple","banana","pineapple"] //returns true;
["grape", "pineapple"] //returns false;
How can I do it in JavaScript?
Personally, I would use the following function:
The "toString()" method will always use commas to separate the values. Will only really work with primitive types.
You could use lodash and do:
Set intersection is done on both collections producing an array of identical elements.
I came up with a solution in node using underscore js like this:
Array .filter() with a nested call to .find() will return all elements in the first array that are members of the second array. Check the length of the returned array to determine if any of the second array were in the first array.