0

I have an array of objects of which I then try to see if an object exists in that array, I have used Array#includes which always gives me false and Array#indexOf which always gives me -1, I tried using $.inArray of jQuery however I'm using React Native which doesn't support jQuery. The thing is the objects look exactly the same and should be the same as I'm just passing the data around, my logs:

Array:

Array [
  Object {
    "age_range": Object {
      "min": 21,
    },
    "city": "San Francisco",
    "first_name": "Michelle",
    "gender": "male",
    "id": "1798537990447153",
    "last_name": "West",
    "locale": "en_GB",
    "name": "Michelle West",
    "option": "Option 5",
    "picture": Object {
      "data": Object {
        "height": 236,
        "is_silhouette": false,
        "url": "https://scontent.xx.fbcdn.net/v/t1.0-1/10430919_1383337475300542_6783404984861635685_n.jpg?oh=0d76f0c209288149c0fb3cd4f09c211a&oe=5AD1C732",
        "width": 236,
      },
    },
    "sub": "Hjj",
    "uid": "xx",
    "updated_time": "2016-08-16T16:48:59+0000",
    "verified": false,
  },
]

Object:

Object {
  "age_range": Object {
    "min": 21,
  },
  "city": "San Francisco",
  "first_name": "Michelle",
  "gender": "male",
  "id": "1798537990447153",
  "last_name": "West",
  "locale": "en_GB",
  "name": "Michelle West",
  "option": "Option 5",
  "picture": Object {
    "data": Object {
      "height": 236,
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/v/t1.0-1/10430919_1383337475300542_6783404984861635685_n.jpg?oh=0d76f0c209288149c0fb3cd4f09c211a&oe=5AD1C732",
      "width": 236,
    },
  },
  "sub": "Hjj",
  "uid": "xx",
  "updated_time": "2016-08-16T16:48:59+0000",
  "verified": false,
}

When using a tool to double check only thing I noticed were some spacing differences maybe cause by different editors, all I'm doing is Arr.includes(obj), what could be causing the problem please and how do I correctly identify if that object exists?

Olli
  • 512
  • 3
  • 6
  • 22

2 Answers2

0

These are two different objects with the same properties. The reason it’s returning false is that it is checking the objects by reference instead of value, which are not equal in this case.

Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97
0

In JavaScript, comparison for equality between objects usually happens by comparing references, not by matching properties and values. Consider the following:

var o1 = {a: 1};
var o2 = {a: 1};
o1 == o2;  // => false
o1 === o2; // => false

In the above example, o1 and o2 are two distinct objects, constructed as separate object literals, that happen to have the same properties, but importantly, they are not the same object. As such, the Array "includes" and "indexOf" methods will not treat them as equal and therefor will not find them:

[o1].includes(o2); // => false
[o1].indexOf(o2);  // => -1

If you want to find an object in an array by matching properties then consider using a library function such as lodash _.find(...) or similar:

_.find([o1], o2); // => o1
maerics
  • 151,642
  • 46
  • 269
  • 291