Skip to content
Advertisement

Javascript: Using `.includes` to find if an array of objects contains a specific object

I am a bit new to javascript ES6, and I am having difficulty understanding why the below is not functioning as expected:

JavaScript

Thanks!

Advertisement

Answer

includes essentially checks if any element === the element you’re searching for. In case of objects, === means literally the same object, as in the same reference (same place in memory), not the same shape.

JavaScript

But if you search for an object that is actually in the array it works:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement