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

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.

var a1 = { name: 'a' }
var a2 = { name: 'a' }

console.log(a1 === a2) // false because they are not the same object in memory even if they have the same data

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

var a1 = { name: 'a' }
var a2 = { name: 'a' }
var array = [a1, a2]

console.log(array.includes(a1)) // true because the object pointed to by a1 is included in this array 


It doesn't work because objects are never the same, each object has its own reference:

Use array.prototype.some instead:

const arr = [{name: 'trent'},{name: 'jason'}];
const obj = {name: 'trent'}; 
const check = arr.some(e => e.name === obj.name);
console.log(check);


One of these

let check = [{name: 'trent'}, {name: 'jason'}]
  .map(item => item.name)
  .includes('trent');

OR

let check = !![{name: 'trent'}, {name: 'jason'}].find(({name})=> name ==='trent')