Categories
JavaScript Answers

How to check if an array contains any element of another array in JavaScript?

Spread the love

To check if an array contains any element of another array in JavaScript, we use the some method.

For instance, we write

const arr1 = ["apple", "grape"];
const arr2 = ["apple", "banana", "pineapple"];
const found = arr1.some((r) => arr2.includes(r));

to call arr1.some with a callback that checks if the item being iterated through r is in arr1 is in arr2 with arr2.includes.

If the callback returns true, then some returns true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.