简体   繁体   English

即使在数组中也找不到对象

[英]Object not found within array even though it is

I have 2 object arrays ( allUsers and friendsOnTrip ). 我有2个对象数组( allUsersfriendsOnTrip )。 Both these arrays are of the same format. 这两个数组的格式相同。 Each object within them contains details of a user (ie firstName and lastName ). 其中的每个对象都包含用户的详细信息(即firstNamelastName )。 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] friendsusersToSeeLocationlocation中,内容完全相同,因此我不确定为什么这些表达式求值为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.

相关问题 object 中的数组返回长度为 0,即使存在元素 - Array within object returning length of 0, even though there are elements present 即使在数组中找到元素,jQuery也不会更改CSS? - jQuery not changing CSS even though element found in array? 即使存在Cookie,也找不到Cookie - Cookie not found even though it exists 为什么这会返回对全局窗口对象的引用,即使&#39;this&#39;在另一个函数内 - Why does this return a reference to the global window object, even though 'this' is within another function 即使已明确定义数组对象并在范围内,array.push()也不起作用 - array.push() does not work even though array object is clearly defined and in scope 即使我正在创建新的引用,javascript 数组对象也会覆盖以前的对象 - javascript array object overwriting previous objects even though I'm creating new references 即使依赖数组 object 保持不变,在单击按钮时重新渲染页面仍保留在 Reactjs 中 - Re-render page on clicking button even though dependency array object remains unchanged remains in Reactjs 即使我从我的 javascript 函数返回一个数组,我也得到一个对象 - I'm getting an object even though I'm returning an array from my javascript function 404 未找到(API url 未找到,即使它 100% 存在) - 404 not found (API url not found even though it 100% exists) Typeof object 不是 function 即使它实现 [[Call]] - Typeof object is not function even though it implements [[Call]]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM