Compare arrays in JavaScript
This post will discuss how to compare arrays in JavaScript. Two arrays are said to be equal if they have the same elements in the same order. The solution should work for nested arrays of any depth.
For example, the array [1, 2, [3, [4, 5]]] is equal to the array [1, 2, [3, [4, 5]]] while it is different from the array [1, 2, [3, 4, 5]].
1. Using Underscore/Lodash Library
If you’re already using the Underscore or Lodash library, consider using the _.isEqual method to test for array equality. It performs a deep comparison between specified values and also works for nested arrays.
|
1 2 3 4 5 6 7 8 9 10 11 |
var _ = require('underscore'); // or lodash var first = [ 1, 2, [3, 4, 5] ]; var second = [ 1, 2, [3, 4, 5] ]; var isEqual = _.isEqual(first, second); console.log(isEqual); /* Output: true */ |
2. Using JSON.stringify() function
Another solution is to convert both arrays into a JSON string and compare their string representation with each other to determine equality. For converting the array to a JSON string, you can use the JSON.stringify() method. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 |
var first = [ 1, 2, [3, 4, 5] ]; var second = [ 1, 2, [3, 4, 5] ]; var isEqual = JSON.stringify(first) === JSON.stringify(second); console.log(isEqual); /* Output: true */ |
The above solution might not work if your array contains a nullish value (i.e., null or undefined) or another object but works fine for other primitive values like numbers and strings.
3. Custom function
Finally, you can write custom logic to determine whether two arrays are equivalent. The following code example shows how to implement this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function isEqual(a, b) { if (a instanceof Array && b instanceof Array) { if (a.length !== b.length) { return false; } for (var i = 0; i < a.length; i++) { if (!isEqual(a[i], b[i])) { return false; } } return true; } return a === b; } var first = [ 1, 2, [3, 4, 5] ]; var second = [ 1, 2, [3, 4, 5] ]; var isEqual = isEqual(first, second); console.log(isEqual); /* Output: true */ |
4. Using String.prototype.toString() function
For a primitive array of numbers and strings, you can simply call toString():
|
1 2 3 4 5 6 7 8 9 |
var first = [ 1, 2, 3, 4, 5 ]; var second = [ 1, 2, 3, 4, 5 ]; var isEqual = first.toString() === second.toString(); console.log(isEqual); /* Output: true */ |
Alternatively, use the Array.prototype.every() to compare each array element with elements of the other array.
|
1 2 3 4 5 6 7 8 9 10 11 |
var first = [ 1, 2, 3, 4, 5 ]; var second = [ 1, 2, 3, 4, 5 ]; var isEqual = first.length === second.length && first.every((value, index) => value === second[index]) console.log(isEqual); /* Output: true */ |
That’s all about comparing arrays 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 :)