0

I am trying to get my head around what is going here. I am search for a matching id using .findIndex() (I am using mongoose). Despite having the exact same output and type, and using == I still return false. However, if I convert both using .toString() it works. Unfortunately typeof does not give me any more information than that both are objects.

Does anyone know why this is happening?

Code

moduleids = Object.values(user.modules.map(a=>[a.id]));
moduleindex = moduleids.findIndex(function(e) {
    console.log("e: " + e);
    console.log("module._id: " + module._id);
    console.log("typeof module._id: " + typeof(module._id));
    console.log("typeof e: " + typeof(e));
    return(e == module._id);
});

Portion of user.modules schema.

modules: [ {
    id: { type: Schema.Types.ObjectId, ref: 'Modules' },
    modality: { type: String },
    selected: { type: Boolean, default: false }
} ]

The module variable is derived from a Modules schema.

Output

e: 5a4ee5d7e10c0a20dcb630e0
module._id: 5a4ee5d7e10c0a20dcb630e0
typeof module._id: object
typeof e: object
false   // This only works if converted to string return(e.toString() == module._id.toString());.
badinvestor
  • 67
  • 1
  • 10
  • 1
    Two distinct objects never compare `==` in JavaScript, even when they contain the same values. – Bergi Jan 05 '18 at 17:37
  • values printed in console may not be actual value and it may be result of toString() method of that object. – kishan Jan 05 '18 at 17:42

0 Answers0