Last updated: Mar 1, 2024
Reading timeยท3 min
for
To check if an array contains any element of another array:
Array.some()
method to iterate over the first array.Array.some()
method will return
true
.const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => { return arr2.includes(element); }); console.log(contains); // ๐๏ธ true
The function we passed to the Array.some() method gets called with each element of the array.
some()
method returns true
, otherwise, false
is returned.const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.includes('pizza')); // ๐๏ธ true console.log(arr1.includes('another')); // ๐๏ธ false
The arrays in the example have a common element, so the
Array.includes() method
returns true
, causing the Array.some()
method to also return true
.
Alternatively, you can use the indexOf()
method.
This is a three-step process:
Array.some()
method to iterate over the first array.Array.indexOf()
method to check if each element is contained in the
second array.Array.indexOf()
method returns a value other than -1
, the arrays
contain common elements.const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; const contains = arr1.some(element => { return arr2.indexOf(element) !== -1; }); console.log(contains); // ๐๏ธ true
If the arrays have a common element, then
Array.indexOf will return
the element's index, otherwise, it returns -1
.
const arr1 = ['pizza', 'cake', 'cola']; console.log(arr1.indexOf('pizza')); // ๐๏ธ 0 console.log(arr1.indexOf('another')); // ๐๏ธ -1
In the code example, there is a common element between the arrays, so the
callback function returns true
and the Array.some()
method also returns
true
.
Alternatively, you can use a for...of
loop.
This is a three-step process:
false
.for...of
loop to iterate over the first array.true
.const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (const element of arr1) { if (arr2.includes(element)) { containsAny = true; break; } } console.log(containsAny); // ๐๏ธ true
Notice that we declared the containsAny
variable using let
.
This is necessary because variables declared using const
cannot be reassigned.
The for...of statement is
used to loop over iterable objects like arrays, strings, Map
, Set
and
NodeList
objects and generators
.
On each iteration, we check if the second array contains the current element.
If the condition is met, we know there is at least 1 common element, so we set
the containsAny
variable to true
and exit the for...of
loop.
The
break
statement terminates the current loop or switch
statement.
for
You can also use a basic for
loop to check if an array contains any element of
another array.
const arr1 = ['pizza', 'cake', 'cola']; const arr2 = ['pizza', 'beer']; let containsAny = false; for (let index = 0; index < arr1.length; index++) { if (arr2.includes(arr1[index])) { containsAny = true; break; } } console.log(containsAny); // ๐๏ธ true
We initialized a boolean variable to false
, just like in the previous code
sample.
On each iteration, we check if the current element is contained in the second array.
If the condition is met, the arrays contain at least one common element, so we break out of the loop.