EN
JavaScript - check if array contains any element of another array
0 points
In this article, we would like to show you how to check if array contains any element of another array in JavaScript.
Quick solution:
xxxxxxxxxx
1
const array1 = ['a', 'b', 'c'];
2
const array2 = ['c', 'd', 'e'];
3
4
const result = array1.some((item) => array2.includes(item));
5
6
console.log(result); // true
Note:
Array
includes()
was introduced in ES6 (around 2016).
In this section, we present an alternative solution that works in older web browsers and Node.js.
xxxxxxxxxx
1
const array1 = ['a', 'b', 'c'];
2
const array2 = ['c', 'd', 'e'];
3
4
const result = array1.some((item) => array2.indexOf(item) >= 0);
5
6
console.log(result); // true