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.

1
  • That's invalid JS code to describe an array: arr1 = ["a","e","i","o","u"]; Please edit your question to fix the code. Commented Nov 26, 2021 at 5:24

3 Answers 3

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)

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

Comments

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

2 Comments

you can use .some instead of filter.
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.
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.