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:
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b', 'c'];
const array2 = ['c', 'd', 'e'];
const result = array1.some((item) => array2.includes(item));
console.log(result); // true
Note:
Arrayincludes()was introduced in ES6 (around 2016).
Alternative solution
In this section, we present an alternative solution that works in older web browsers and Node.js.
// ONLINE-RUNNER:browser;
const array1 = ['a', 'b', 'c'];
const array2 = ['c', 'd', 'e'];
const result = array1.some((item) => array2.indexOf(item) >= 0);
console.log(result); // true