1

I have this code:

const arr = [
{name:"Bill", age:11},
{name:"Bill", age:11}
]
console.log(arr.includes({name:"Bill", age:11}))

Here i want to check if the array includes {name:"Bill", age:11}.
Why i get false? And how to make this checking using includes?

6
  • you compare an object with other objects. they have all different object references. Commented Aug 10, 2020 at 8:10
  • It can help How to determine equality for two JavaScript objects? Commented Aug 10, 2020 at 8:11
  • Object comparison in JavaScript Commented Aug 10, 2020 at 8:11
  • @NinaScholz, what could be the solution? Could you show please? Commented Aug 10, 2020 at 8:11
  • There is no built-in way to check equality on objects, because the comparison is done by reference (two objects can have different references even if they have the same contents). I suggest you either do the comparison key by key, or use a library method like _.isEqual(). Commented Aug 10, 2020 at 8:15

2 Answers 2

8

The includes() method compares objects by reference and not by value. In your case the three objects have three different references although they have the same properties and the same values in them.

const bill = { name: 'Bill', age: 11 }
const arr = [bill, { name: 'Jane', age: 18 }]

arr.includes(bill) // true (same reference)
arr.includes({ name: 'Bill', age: 11 }) // false (different reference)

If you want to find objects by value, you can use the find() method and pass a filter function which checks if each property of the object matches your criteria.

const arr = [{name:"Bill", age:11}, {name:"Jane", age:18}]
const exists = Boolean(arr.find(x => x.name === 'Bill' && x.age === 11))

// or even simpler using the `some()` method
const exists = arr.some(x => x.name === 'Bill' && x.age === 11)
Sign up to request clarification or add additional context in comments.

2 Comments

An alternative to combining Boolean() and Array.find() is simply using Array.some().
Thanks for reminding me about this method. I've added it to the examples.
3

You can create a custom array prototype method for this like includesObj

const arr = [
{name:"Bill", age:11},
{name:"Bill", age:11}
]

Array.prototype.includesObj = function(obj) {
   for(let i = 0; i < this.length; i++) {
      if(JSON.stringify(this[i], Object.keys(this[i]).sort()) === JSON.stringify(obj, Object.keys(obj).sort())) return true;
   }
   return false;
}

console.log(arr.includesObj({name: "Bill", age: 11}))
console.log(arr.includesObj({age: 11, name: "Bill"}))
console.log(arr.includesObj({name: "Bob", age: 11}))

4 Comments

if you want to have it relieable, you also need to add Object.keys(this[i]).sort() and Object.keys(obj).sort() as second parameter of stringify
@RenéDatenschutz sure? i get twice true at console log
but it would fail on console.log(arr.includesObj({age: 11, name: "Bill"})) if you don't do that
@RenéDatenschutz it was a typo by myself, it works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.