"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;
}
}