Reputation: 151
I've got a problem on Javascript.
var reponseValide1 = [{
title: "test"
}, {
text: "test"
}];
var reponseValide2 = [{
title: "test"
}];
console.log(reponseValide1.indexOf(reponseValide2[0]));
The above code logs false
. I don't understand why. reponseValide2
does contain the same object, with same variables and types. Can you help me understand why, please?
Upvotes: 5
Views: 3786
Reputation: 69346
"reponseValide2" does contain the same object, with same variables and type
No, it doesn't! It might look like it, but those two are different objects. Equality for object means checking if they are the same instance (i.e. they point to the same address in memory).
Take this code for example:
a = {x: 1}
b = {x: 1}
console.log(a == b)
> false
In the moment of creation, the {x: 1}
object is created in memory, and its address is assigned to a
. When creating the second object, it will be put in a different memory region, and b
will contain a different address than a
.
If a
and b
were the same object, then modifying a
would mean modifying b
too! For example:
a = {x: 1}
b = a
a.x = 2
console.log(b.x)
> 2
If you really want to have the same object in both your arrays, then you'll have to create it first, then add it to both of them, like this:
var myObj = {
title: "test"
};
var reponseValide1 = [
myObj,
{text: "test"}
];
var reponseValide2 = [myObj];
console.log(reponseValide1.indexOf(reponseValide2[0]));
However, if you don't want to have the same object, but you want to check if they are equal, you'll have to do it manually, checking for example the text
property, like this:
var reponseValide1 = [{
title: "test"
}, {
text: "test"
}];
enter code here
var reponseValide2 = [{
title: "test"
}];
for (var i = 0; i < responseValide1.length; i++) {
if (responseValide1[i].title == responseValide2[0].title) {
console.log(i);
break;
}
}
Upvotes: 0
Reputation: 2039
it is because these objects doesn't referencing to same object. You can alternatively use this code to get matched index.
reponseValide1.findIndex(obj => obj.title == reponseValide2[0].title)
Upvotes: 1
Reputation: 5797
Under the hood, Array.prototype.indexOf()
is basically performing a strict equality check on each element and returning the index of the first positive result.
In JavaScript, equality of objects is determined on the basis of whether they share the same reference.
An expression comparing Objects is only true if the operands reference the same Object.
To compare objects based on contents, a more involved solution is required - the simplest typically being to wrap each object in JSON.stringify()
(although this is very inefficient). More efficient solutions tend to involve comparison of individual keys and values.
See below for a rough demonstration.
const A = {x: 'y'}
const B = {x: 'y'}
const AB = [A, B]
console.log(AB.indexOf(A)) // => 0
console.log(AB.indexOf(B)) // => 1
console.log(A === B) // => false
console.log(A === A) // => true
console.log(JSON.stringify(A) === JSON.stringify(B)) // => true
Upvotes: 3