Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't equality check work with arrays [duplicate]

Tags:

javascript

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?

like image 671
Jacksonkr Avatar asked Jun 13 '15 15:06

Jacksonkr


People also ask

Why comparing arrays using == does not work?

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.

Can you use == to compare arrays?

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.

How do you check if two arrays are equality?

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.

Does == work for arrays 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.

How to test equality of values in an array or object?

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:

How to check if two arrays are equal in Java?

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.

How to check if two arrays have the same content?

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).

How to compare if two values are equal in JavaScript?

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.


1 Answers

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.

like image 155
uraimo Avatar answered Sep 21 '22 01:09

uraimo