admin管理员组文章数量:1406312
I've got a problem on Javascript.
var reponseValide1 = [{
title: "test"
}, {
text: "test"
}];
var reponseValide2 = [{
title: "test"
}];
console.log(reponseValide1.indexOf(reponseValide2[0]));
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?
-
it may return
-1
instead offalse
. it does not look for the same object reference. it looks for another object. – Nina Scholz Commented Oct 29, 2018 at 11:09 -
technically it logs
-1
– Ayush Gupta Commented Oct 29, 2018 at 11:09 - ""reponseValide2" does contain the same object" - no, it doesn't. "with same variables and type" - that doesn't make it the same object. indexOf uses strict parison, developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – misorude Commented Oct 29, 2018 at 11:10
-
Do we have a canonical answer for
Why does array.find({prop: "value"}) doesn't get {prop: "value"}
type of questions? – VLAZ Commented Oct 29, 2018 at 11:12 - One object is never going to equal another, the references are different. You can only pare primitive types like that. – nanobar Commented Oct 29, 2018 at 11:51
3 Answers
Reset to default 3Under 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 paring Objects is only true if the operands reference the same Object.
To pare 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 parison 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
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)
"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;
}
}
本文标签: javascriptArray indexOf checking for object equalityStack Overflow
版权声明:本文标题:javascript - Array indexOf checking for object equality - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745006755a2637309.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论