Why Doesn't Equality Check Work with Arrays

Why doesn't equality check work with arrays

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.

Triple equal signs return false for arrays in javascript. why?

They are not equal because a new array is being created in each of these statements, each being a brand new array object with just identical contents. If you create two new objects:

var a = {};
var b = {};

a === b // false

When you create new objects, arrays, functions, etc., a brand new object is placed into memory. Creating a new object with the same internals as another object will not magically cause that object to point to one that already exists. The objects may look the same, but they do not point to the same instance. Now if your statement had been like so:

var arr = ['asdf'];

arr === arr; // true

This is obviously be true. === is strict equality, not an identity operator. When objects are ran through a strict equality operator, they are checked to see if they point to the same reference. As I explained earlier, each time you use new Array or [] that a brand new object will be created, each being a new and different reference. So there is no way that two arrays, or any object, can come out === being true unless they point to the exact same array. Just because two objects are being created with identical contents does not mean that they point to the same object, just two identical, but different objects.

Think of constructing functions:

var Car = function (color) {
this.color = color;
};

var ford = new Car('green');
var chevy = new Car('green');
var toyota = ford;

ford === chevy // false

Just because you are using the same constructor does not mean that every time you call it the same object gets returned. Rather, a new object will be returned every time. Just because both cars are green doesn't mean it's the same car.

ford === toyota // true

This is now true because both variables point to the exact same Car reference.

Why new Array() is not equal to []?

Because they are two different references. They can be two arrays with no elements, but they are two completely different objects on the heap.

chai test array equality doesn't work as expected

For expect, .equal will compare objects rather than their data, and in your case it is two different arrays.

Use .eql in order to deeply compare values. Check out this link.

Or you could use .deep.equal in order to simulate same as .eql.

Or in your case you might want to check .members.

For asserts you can use .deepEqual, link.

Java: Checking equality of arrays (order doesn't matter)

  • Arrays.sort(s1);
  • Arrays.sort(s2);
  • Arrays.equals(s1,s2);

In case you do not want to modify the original arrays

 Arrays.equals( Arrays.sort( Arrays.copyof(s1,s1.length)),
Arrays.sort( Arrays.copyof(s2,s2.length)) );

Arrays.sort() uses an optimized quick sort which is nlog(n) for average but O(n2) in worst case. From the java docs. So the worst case it will O(n2) but practically it will be O(nlogn) for most of the cases.

The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

Check if all values of array are equal

const allEqual = arr => arr.every( v => v === arr[0] )
allEqual( [1,1,1,1] ) // true

Or one-liner:

[1,1,1,1].every( (val, i, arr) => val === arr[0] )   // true

Array.prototype.every (from MDN) :
The every() method tests whether all elements in the array pass the test implemented by the provided function.

Why an empty array equals to an empty string while not to another empty array in javascript?

Because of JavaScript coercion.

[] is loosely equated to "" and thus coerced to string using [].toString() which is equal to "".

And why does [] == [] and [] === [] return false:

the == and === comparison rules if you’re comparing two non-primitive values, like objects (including function and array). Because those values are actually held by reference, both == and === comparisons will simply check whether the references match, not anything about the underlying values.

var a = [1,2,3];
var b = [1,2,3];

var c = "1,2,3";

a == c; // true
b == c; // true
a == b; // false

arrays are by default coerced to strings by simply joining all the values with commas (,) in between.



Related Topics



Leave a reply



Submit