Compare Arrays Using Array Prototype Every in JavaScript
We are required to write a JavaScript function that takes in two arrays of literals. Then our function should return true if all the elements of first array are included in the second array, irrespective of their count, false otherwise.
We have to use Array.prototype.every() method to make these comparisons.
Example
The code for this will be −
const arr1 = [0, 2, 2, 2, 1]; const arr2 = [0, 2, 2, 2, 3]; const compareArrays = (arr1, arr2) => { const areEqual = arr1.every(el => { return arr2.includes(el); }); return areEqual; }; console.log(compareArrays(arr1, arr2));
Output
And the output in the console will be −
false
Advertisements