Array.includes() to find object in array

This is because the includes checks to see if the object is in the array, which it in fact is not:

> {a: 'b'} === {a: 'b'}
false

This is because the test for equality is not testing if the objects are the same, but whether the point to the same object. Which they don't.


Array.includes compares by object identity just like obj === obj2, so sadly this doesn't work unless the two items are references to the same object. You can often use Array.prototype.some() instead which takes a function:

const arr = [{a: 'b'}]
console.log(arr.some(item => item.a === 'b'))

But of course you then need to write a small function that defines what you mean by equality.


Its' because both of the objects are not the same. Both are stored at different place in memory and the equality operation results false.

But if you search for the same object, then it will return true.

enter image description here

Also, have a look at the below code, where you can understand that two identical objects also results false with the === operator.

For two objects to return true in ===, they should be pointing to same memory location.

enter image description here