'Comparing array of ObjectIds to ObjectId

I have a simple use case that used to work in my code, however now I've had to make changes to accommodate an issue I am now having. Looking to get some insights into why this has changed and requires extra check if anyone can help. My scenario is as described.

I have an array userBadgeIds which holds the following: [ new ObjectId("59f347fca0f50c0004b5a3ac") ]

I used to do the following line: const exists = userBadgeIds.includes(badge._id) which worked perfectly and told me if the input badge._id was in the array. Both are mongoose ObjectId so comparing equivalent in this way seemed perfect (again, it worked!).

However, for whatever reason I now have to compare them in the following way:

      const exists = userBadgeIds.map(String).includes(badge._id.toString())

Now it works again. Is there something I'm missing about how the .includes compares equivalence or something I'm missing about comparing ObjectId specifically around ObjectId[]?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source