Sometimes, we want to check if array contains all elements of another array with JavaScript.
In this article, we’ll look at how to check if array contains all elements of another array with JavaScript.
How to check if array contains all elements of another array with JavaScript?
To check if array contains all elements of another array with JavaScript, we call the every
method.
For instance, we write
const array1 = [1, 2, 3];
const array2 = [1, 2, 3, 4];
const checker = (arr, target) => target.every((v) => arr.includes(v));
console.log(checker(array2, array1));
to call target.every`` with a callback that checks if every item
vin
targetis in the
arrarray with
includes`.
Then we call checker
with the 2 arrays to check.
Conclusion
To check if array contains all elements of another array with JavaScript, we call the every
method.