Determine if two arrays are equal in JavaScript
This post will discuss how to determine if two arrays are equal in JavaScript.
To check if two arrays are equal in JavaScript, we need to compare their lengths and their elements at the same positions. We cannot use the operators == or === for array equality comparisons. They will only work if the two arrays are the same object in memory, which is usually not the case. However, there are several ways to compare the actual contents of the arrays in JavaScript, depending on how we define equality and the type of arrays we are dealing with. Here are some of the common functions:
1. Using Array.every() function
The Array.every() function tests whether all elements in the array pass a test implemented by a provided function. We can use this function to check whether each element in the first array is equal to the corresponding element in the second array, using the strict equality operator (===). Here’s an example:
|
1 2 3 4 5 6 7 |
let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; let isEqual = (arr1.length === arr2.length) && arr1.every((value, index) => value === arr2[index]); console.log(isEqual); // true |
However, this function also fails for arrays that contain objects or other non-primitive values that are not strictly equal. This function is also not very flexible as it does not allow us to customize how we want to compare the elements of the arrays.
2. Using a loop
We can iterate over the arrays and compare each element with the corresponding one in the other array. This can be done using the strict equality operator (===) for arrays containing primitive values, such as numbers or strings, and using a flag variable to indicate whether the arrays are equal or not. We can use any kind of loop, such as a for loop, a while loop, or a forEach loop. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function isEqual(arr1, arr2) { // if the arrays have different lengths, they are not equal if (arr1.length !== arr2.length) { return false; } // loop through the arrays and compare each element for (let i = 0; i < arr1.length; i++) { // if the elements are not equal, the arrays are not equal if (arr1[i] !== arr2[i]) { return false; } } // if the loop finishes without returning false, the arrays are equal return true; } let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; console.log(isEqual(arr1, arr2)); // true |
This approach is recommended only if we need a custom solution that suits our specific needs. Currently, the function works for arrays that contain primitive values that can be compared using strict equality operator.
3. Using JSON.stringify() function
We can use the JSON object functions to convert the arrays to strings and then compare them. The JSON.stringify() function converts an array into a JSON string, which can then be compared using the strict equality operator (===). This function works for arrays that contain only primitive values (such as numbers, strings, booleans, null, and undefined) and that have the same order of elements. However, this function fails for arrays that contain objects, functions, symbols, or other non-primitive values, as well as arrays that have different orders of elements. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function isEqual(arr1, arr2) { // convert the arrays to strings using JSON.stringify let str1 = JSON.stringify(arr1); let str2 = JSON.stringify(arr2); // compare the strings using === operator return str1 === str2; } let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; console.log(isEqual(arr1, arr2)); // true |
Note that JSON.stringify() does not guarantee a consistent order of properties for objects, and it omits some values that is not JSON compatible. Also it involves creating new strings and comparing them character by character which is not very efficient.
4. Using third-party libraries
We can use a library like Lodash or Underscore.js that provides a function to check if two arrays are equal. These libraries can handle any type of array, including nested objects and arrays. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// import the library const _ = require("lodash"); // or const _ = require("underscore"); // use the _.isEqual function to compare the arrays function isEqual(arr1, arr2) { return _.isEqual(arr1, arr2); } let arr1 = [1, 2, 3]; let arr2 = [1, 2, 3]; console.log(isEqual(arr1, arr2)); // true |
That’s all about determining if two arrays are equal in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)