Javascript - arrays comparison [duplicate] - javascript

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Javascript: Equality Comparison between two Object/ Array
(3 answers)
Closed 8 years ago.
var array1=[1,2,3];
var array2=[1,2,3];
alert((array1<array2)+(array1==array2)+(array1>array2));
As alert returns 0, array1 is not greater, not less and not equal to array2.
The question is:
How does array1 relates to array2?
Edit: my question is: How does array1 relates to array2?

The two array array1 and array2 are never equal as they are different instances.
If you want to compare them, you can do:
array1.join() == array2.join() // true
And BTW, the alert() doesn't alert false it alerts 0

Weird thing about arrays, they are never equal since they're not the same object.
Although, arrays act strange when you compare them with < or >
We can use this weird behavior to our advantage to check whether two arrays have the same content:
function arraysEqual(a, b) { return !(a < b || b < a); }
Or
function arraysEqual(a, b) { return !(a < b) && !(b < a); }
Which means, if the no array is bigger than the other, they're equal.
Source: How to check if two arrays are equal with JavaScript?

Related

Why my two arrays with string elements are not equal? [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(61 answers)
Closed 6 months ago.
I have an array which is called ids_to_remove. When I type it in browser inspect, I get the following result:
ids_to_remove
(2) ['1', '2']
When I type in the same array I get:
['1','2']
(2) ['1', '2']
When I compare the elements I get:
ids_to_remove[0]==='1'
true
ids_to_remove[1]==='2'
true
also
typeof(ids_to_remove[0])
'string'
But when I compare the arrays:
ids_to_remove===['1','2']
false
Does anyone have any idea why these two 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. The equality operator will only test if two objects are actually exactly the same instance (e.g. myObjVariable==myObjVariable, works for null and undefined too).
you can simply use this function:
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
This function only works for simple non primitive type arrays.

Searching for a specific array in an array of arrays [duplicate]

This question already has answers here:
Javascript indexOf for an array of arrays not finding array
(6 answers)
Closed 1 year ago.
const a = [[1,1], [2,1], [3,1]];
console.log(a);
console.log(a.includes([1,1]));
> false
Can someone explain to me why the above outputs false? For what its worth, this occurs in any of the search functions (.find, .indexof, etc), as well as if I try [1,1] in a. I'm clearly missing something about how multidimensional array searching works in javascript.
This is basically how equal to operator works in JavaScript.
If we try to compare two arrays
[1]=== [1] // false
equality operator can compare primitive values(number,string Boolean). But in case of arrays and objects it compares for reference. So here we are comparing two different references so it is coming as false.
For same reason you are getting false. You have to write customer logic to compare arrays.

Map.prototype.has() not working with arrays as keys? [duplicate]

This question already has answers here:
Using Array objects as key for ES6 Map
(4 answers)
Closed 3 years ago.
How can i make Map.has() work with arrays?
Why does this example output false?
let test = new Map();
test.set(["a", "b"], "hi");
console.log(test.has(["a", "b"]));
It doesn't work because your two arrays are not referring to the same object. The array contents are identical, but the arrays themselves are not.
It will work if you use the same object to both set and retrieve the value:
let test = new Map();
let key = ["a", "b"];
test.set(key, "hi");
console.log(test.has(key)); // true
Map key equality
Key equality is based on the sameValueZero algorithm: NaN is
considered the same as NaN (even though NaN !== NaN) and all other
values are considered equal according to the semantics of the ===
operator. In the current ECMAScript specification -0 and +0 are
considered equal, although this was not so in earlier drafts. See
"Value equality for -0 and 0" in the Browser compatibility table for
details.
Since in JS comparing two references will never turn out to be true, so you need to store reference of key in some variable and use when checking on Map again
console.log([] === [])
console.log({} === {})
let test = new Map();
let key = ["a","b"]
test.set(key, "hi");
console.log(test.has(key));

Why doesn't anArray === [] evaluate to true when anArray.length === 0 does in Javascript? [duplicate]

This question already has answers here:
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 6 years ago.
Can you explain this weird JavaScript behavior?
First :
[] === [] false
[] == [] false
Why false? The object are identical, thus it should return true.
Second :
[] !== [] true
[] != [] true
Again, why true? the objects are identical.
They're not identical. Object identity is defined by both operands pointing to the same instance.
var a = [],
b = [];
a == b; // false
a == a; // true
Two literals always evaluate to two different instances, which are not considered equal. If you are looking for structural equivalence, see How to compare arrays in JavaScript?.
Objects are not identical. In this case you compare the references to the objects. Easily speaking you compare the addresses in memory where these objects are located. This rule doesn't relate to primitives where you compare the actual values.

Weird behavior of comparison operator JavaScript when using empty array [duplicate]

This question already has answers here:
Triple equal signs return false for arrays in javascript. why?
(4 answers)
Closed 6 years ago.
Can you explain this weird JavaScript behavior?
First :
[] === [] false
[] == [] false
Why false? The object are identical, thus it should return true.
Second :
[] !== [] true
[] != [] true
Again, why true? the objects are identical.
They're not identical. Object identity is defined by both operands pointing to the same instance.
var a = [],
b = [];
a == b; // false
a == a; // true
Two literals always evaluate to two different instances, which are not considered equal. If you are looking for structural equivalence, see How to compare arrays in JavaScript?.
Objects are not identical. In this case you compare the references to the objects. Easily speaking you compare the addresses in memory where these objects are located. This rule doesn't relate to primitives where you compare the actual values.

Categories