0

For example, I have 2 arrays:

arr1 = ["a","e","i","o","u"];
arr2 = ["h", "e", "l", "l", "o"];

I would like to find whether any of the elements in arr2 contain any of the elements that are already available in arr1.

  • That's invalid JS code to describe an array: `arr1 = ["a","e","i","o","u"];` Please edit your question to fix the code. – Andy Nov 26 '21 at 05:24

3 Answers3

4

Use Array.some to check whether one of the items in arr2 is included in arr1:

arr1 = ["a", "e", "i", "o", "u"];
arr2 = ["h", "e", "l", "l", "o"];

const res = arr2.some(e => arr1.includes(e))
console.log(res)
Spectric
  • 30,714
  • 6
  • 20
  • 43
2

Find the array with below filter function

let array1 = ["a","e","i","o","u"]; 
let array2 = ["h", "e", "l", "l", "o"];

const filteredArray = array1.filter(value => array2.includes(value));

filteredArray.length will give you the answer

Bansi29
  • 1,053
  • 6
  • 24
Keerthi Vijay
  • 356
  • 2
  • 9
  • 2
    you can use `.some` instead of filter. – Nishant Nov 26 '21 at 05:25
  • 1
    The guy said they wanted to know "whether" any of them are in the other array. It depends on your use case, but yeah it seems like they don't care about the actual values inside the arrays. – DexieTheSheep Nov 26 '21 at 05:30
1

Try this.

arr1 = ["a","e","i","o","u"]; 
arr2 = ["h", "e", "l", "l", "o"];

let res = arr1.some(el1 => {
    return arr2.some(el2 => el1 === el2);
});

console.log(res) // true
ATLANT
  • 68
  • 9