[英]Object not found within array even though it is
I have 2 object arrays ( allUsers
and friendsOnTrip
). 我有2个对象数组(
allUsers
和friendsOnTrip
)。 Both these arrays are of the same format. 这两个数组的格式相同。 Each object within them contains details of a user (ie
firstName
and lastName
). 其中的每个对象都包含用户的详细信息(即
firstName
和lastName
)。 I am trying to make it that if an object in one array is not in the other one, then push that object to a new array, otherwise don't. 我试图使一个数组中的一个对象不在另一个数组中,然后将该对象推到一个新的数组中,否则不要这样做。
allUsers.forEach((user) => {
if (friendsOnTrip.indexOf(user) <= -1) {
this._friendsNotOnTrip.push(user);
}
});
The problem is that even if the object user
seems like it is in friendsOnTrip
, then the expression of: 问题是,即使对象
user
看起来像在friendsOnTrip
,也可以使用friendsOnTrip
表达式:
if (friendsOnTrip.indexOf(user) <= -1)
...will still evaluate to true
(which is wrong) so I end up with objects within this._friendsNotOnTrip
that shouldn't be there. ...仍将评估为
true
(这是错误的),因此我最终在this._friendsNotOnTrip
了不应存在的对象。
An example of one of the objects: 对象之一的示例:
{
email: "foo@bar.com",
firstName: "foo",
lastName: "bar",
key: "123456789",
friends: [
"987654321",
"246808642"
],
location: {
lng: -1.24567,
lat: 50.9865
},
usersToSeeLocation: [
"987654321"
]
}
The object at position 0 in allUsers
and the object at position 0 in friendsOnTrip
are the same object. allUsers
位于位置0的对象和friendsOnTrip
中位于位置0的对象是同一对象。 I tested the individual attributes and got the following results: 我测试了各个属性,并得到以下结果:
console.log(allUsers[0].firstName === friendsOnTrip[0].firstName); //true
console.log(allUsers[0].lastName === friendsOnTrip[0].lastName); //true
console.log(allUsers[0].email === friendsOnTrip[0].email); //true
console.log(allUsers[0].friends === friendsOnTrip[0].friends); //false
console.log(allUsers[0].key === friendsOnTrip[0].key); //true
console.log(allUsers[0].location === friendsOnTrip[0].location); //false
console.log(allUsers[0].usersToSeeLocation === friendsOnTrip[0].usersToSeeLocation); //false
console.log(allUsers[0] === friendsOnTrip[0]); //false
Looking inside friends
, usersToSeeLocation
, and location
in both allUsers[0]
and friendsOnTrip[0]
, the contents are the exact same so I am unsure as to why those expressions are evaluating to false
. 在
allUsers[0]
和friendsOnTrip[0]
friends
, usersToSeeLocation
和location
中,内容完全相同,因此我不确定为什么这些表达式求值为false
。
You can't compare objects like this. 您无法比较这样的对象。 For example
例如
[{a: 1, b:2}].indexOf({a: 1, b:2})
returns -1. 返回-1。 Instead you should search for a specific property, something like
相反,您应该搜索特定的属性,例如
allUsers.forEach((user) => {
if (friendsOnTrip.map(u => u.key).indexOf(user.key) <= -1) {
this._friendsNotOnTrip.push(user);
}
});
"Looking inside friends, usersToSeeLocation, and location in both allUsers[0] and friendsOnTrip[0], the contents are the exact same so I am unsure as to why those expressions are evaluating to false." “查看朋友,usersToSeeLocation和allUsers [0]和friendsOnTrip [0]中的位置,它们的内容是完全相同的,因此我不确定这些表达式为何被评估为false。”
that's because they are not the same. 那是因为它们不一样。 The have the same value BUT they are not the same object!
具有相同的值,但它们不是同一对象! that's why your "===" returns false You have to compare all indexes one by one
这就是为什么您的“ ===”返回false您必须一一比较所有索引
you can do array1.join() === array2.join() to do your comparition faster 您可以执行array1.join()=== array2.join()以更快地进行比较
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.