If you are new to the concept of array equality, each section of the article would be useful. However, If you are here to refresh your knowledge, jump straight to the implementation. Go to methods
Table of Contents
- Parting words
- Other Related Concepts
Strict Equality With ===
Given 2 values x and y, the strict equality checks for equality in the following way:x === y
- Check the types of x and y. If they are of different types, return false.
- If x and y are numbers, it checks if either of x or y is NaN, and returns false if one is NaN. If both x and y are either +0 or -0, return true. Otherwise, it also checks to see if they are the same number.
- If x and y are both null or both undefined, it returns true.
- If x and y are both booleans, strings, or symbols, then it compares them by value.
- If x and y are both objects, it returns true if and only if they reference the same object.
Abstract Equality With ==
Here is a brief overview of how the == operator compares if x and y are equal. A
- If x and y are of the same type, check if x === y
- If x and y are both either null or undefined, return true.
- If x is a number and y is a string, convert y to a number and then compare using ===. Similarly, if x is a boolean or string, and y is a number, convert x to a number.
- If x or y is a boolean, convert the other value of a number and compare them.
- If x is an object and y is a symbol, string, or number, try to convert x to a primitive using valueof() and then compare using ===.
In general, you should always use === rather than == unless you're confident about what you are doing and have an idea of the expected outcome.
Although there are 4 ways to check if two arrays are equal, none of them consider the deep equality (where you compare even the contents of your objects recursively until all you need to compare are the primitive fields) between the arrays.
1) Both arrays have the same length and their values are equalIn this method, we compare if each value of a is equal to the value of b. We have to keep in mind that this will work well if all the values of arrays a and b are primitives and not objects.
2) Deep Equality with POJOs (Plain old java object)The function arrayEquals() works well for primitive objects as seen above, but falls short if you want to compare arrays containing objects by value. Let’s take a look at the example below:
One simple way to go about this with minimal code and no external libraries is to use compare them by their JSON.stringify() output
However, there is one edge case. Since undefined is not a valid JSON value, JSON.stringify() converts undefined to null. So the JSON.stringify() outputs of the arrays shown below will be the same even though they aren’t the same in reality.
3) Comparing arrays to check for equality using Lodash’s isequal()As discussed above, using JSON.stringify() to compare array equality not only causes the undefined vs null quirk but also doesn't take into account object types. As far as JSON.stringify() is concerned, an object with a toJSON() function that returns 25 is the same as the number 25.
Similarly, a custom object is also considered to be same as POJO while using JSON.stringify()
On the other hand, using Lodash’s isequal() function takes all of this into account
To conclude, to compare arrays to check for equality, Lodash's isEqual() function is the way to go if you need all the bells and whistles of checking that objects have the same class. The JSON.stringify() approach works well for POJOs, just make sure you take into account null.