• Post category:JavaScript
  • Reading time:2 mins read

Today, we’ll show you how to find if two arrays contain any common item in JavaScript.

Let’s assume that we have two arrays called array1 and array2. Now if we want to check if array2 contains any element of array1 then we have several ways to verify that. But we will see the best of them.

Let’s use the Array method called some() to check it.

The some() method tests whether at least one element in the array passed the check. This method will return the Boolean value.

const array1 = ['item_1', 'item_2', 'item_3'];
const array2 = ['item_10', 'item_30', 'item_2'];
const array3 = ['item_28', 'item_74', 'item_21'];

const isValidArray2 = array1.some(x => array2.includes(x));
const isValidArray3 = array1.some(x => array3.includes(x));

console.log(isValidArray2); // true
console.log(isValidArray3); // false

I hope you find this article helpful.
Thank you for reading. Happy Coding..!!

Leave a Reply