I started with:
"1:2".split(':') == ["1","2"]; // false
Then tried:
[1,2] == [1,2]; // false
and ultimately:
[] == []; // false
I've since found that:
"1:2".split(':').toString() == [1,2].toString(); // true
So I've solved my initial issue (kind of) but why can't arrays match each other?
To test for reference equality, use the reference equality operators, == and != . Because the effect of using Object. equals() to compare two arrays is often misconstrued as content equality, and because a better alternative exists in the use of reference equality operators, the use of the Object.
While comparing two arrays we can not use “==” operator as it will compare the addresses of the memory block to which both the arrays are pointing.
The Arrays. equals() method checks the equality of the two arrays in terms of size, data, and order of elements. This method will accept the two arrays which need to be compared, and it returns the boolean result true if both the arrays are equal and false if the arrays are not equal.
Javascript arrays are objects and you can't simply use the equality operator == to understand if the content of those objects is the same.
In order to test equality of values in an array or object you need to do a 'deep comparison' - for Arrays this will traverse both arrays to compare index and value to see if both are equal - for objects it will traverse every key and makes sure the value is the same. For more on deep comparisons you can check out the underscore.js annotated source:
If you need to check if two array are equals i'd recommend to just traverse both arrays and verify that all the elements have the same value (and that the two array have the same length). Regarding custom objects equality i'd build instead a specific equals function and i'd add it to the prototype of your class.
Javascript arrays are objects and you can't simply use the equality operator == to understand if the content of those objects is the same. The equality operator will only test if two object are actually exactly the same instance (e.g. myObjVariable==myObjVariable, works for null and undefined too).
There are technically 4 ways to compare if two values are equal in javascript. Of these, the two most common methods are the == operator, known as abstract equality and the === operator, known as strict equality. Before diving into checking for array equality, let us understand how these equality operators work in javascript.
Javascript arrays are objects and you can't simply use the equality operator ==
to understand if the content of those objects is the same. The equality operator will only test if two object are actually exactly the same instance (e.g. myObjVariable==myObjVariable
, works for null
and undefined
too).
If you need to check if two array are equals i'd recommend to just traverse both arrays and verify that all the elements have the same value (and that the two array have the same length).
Regarding custom objects equality i'd build instead a specific equals
function and i'd add it to the prototype of your class.
Considering that in the end you converted both arrays to a String
and tested equality of the resulting strings, you could one day consider using a similar but more generic technique you'll find described in more than a few places:
JSON.stringify(OBJ1) === JSON.stringify(OBJ2)
Well, don't.
While this could work if the order of the properties will always the same for those object instances, this leaves the door open for extremely nasty bugs that could be hard to track down. Always favor a more explicit approach and just write a clean and readable function that will test for equality checking all the required fields.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With