0

I have 2 object arrays (allUsers and friendsOnTrip). Both these arrays are of the same format. Each object within them contains details of a user (i.e. firstName and 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:

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.

An example of one of the objects:

{ email: "[email protected]", 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. 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.

8
  • 1
    They're not the same object. They hold the same information but they're still deferent object with different space in memory. Commented Jan 24, 2017 at 12:37
  • 1
    Possible duplicate of Object comparison in JavaScript Commented Jan 24, 2017 at 12:38
  • Do both the Objects have same keys as well as values?? If so then you can try with JSON.stringify both the Objects and then compare. If both the Objects have same key but having different values then It must be return false. Commented Jan 24, 2017 at 12:38
  • How to tell if two objects are different? What are the keys that only identify one object from the other? Commented Jan 24, 2017 at 12:39
  • 1
    @Ashish there is no guarantee the keys have the same order, so JSON.stringify comparisons are unstable Commented Jan 24, 2017 at 12:40

2 Answers 2

3

You can't compare objects like this. For example

[{a: 1, b:2}].indexOf({a: 1, b:2}) 

returns -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); } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

possibly, the email.
you could also create a string of multiple properties by concatenating them
This has worked, thank you! I had completely forgotten about using .map()
0

"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."

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

you can do array1.join() === array2.join() to do your comparition faster

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.