Check if an array contains any element of another array in JavaScript

arrays, javascript

Solution

Vanilla JS

ES2016:

const found = arr1.some(r=> arr2.includes(r))

How it works

`some(..)` checks each element of the array against a test function and returns true if any element of the array passes the test function, otherwise, it returns false. `includes(..)` both return true if the given argument is present in the array.

Problem

I have a target array `["apple","banana","orange"]`, and I want to check if other arrays contain any one of the target array elements. For example: ``` ["apple","grape"] //returns true; ["apple","banana","pineapple"] //returns true; ["grape", "pineapple"] //returns false; ``` How can I do it in JavaScript?

Original source

Related problems