0

I have two objects which I am comparing. I have an array of objects, and I am trying to see if the array contains a certain object. I think it should be finding it, but it is not. Here is the object in the array:

Array Object

and here is the object I am looking for:

Match Object

I am using:

if (collection.indexOf(object) !== -1) {
  //do something  
}

And it returns an index of -1, even though it seems to me that the objects match. Am I missing something here?

  • Can you add your array and object codes in your question please ? – Magicprog.fr Jun 29 '15 at 13:48
  • possible duplicate of [Object comparison in JavaScript](http://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – Ben Fortune Jun 29 '15 at 13:48
  • possible duplicate of [Find object by id in array of javascript objects](http://stackoverflow.com/questions/7364150/find-object-by-id-in-array-of-javascript-objects) – nils Jun 29 '15 at 13:49
  • possible duplicate of http://stackoverflow.com/questions/12604062/javascript-array-indexof-doesnt-search-objects – Gerard Sexton Jun 29 '15 at 13:49
  • `indexOf()` will work only for elementary types like numbers, strings, boolean etc. It does not do deep comparison to match objects. – techfoobar Jun 29 '15 at 13:50
  • Ok, I think that answers my question (I need to use another way to compare). If you post as an answer I'll accept. –  Jun 29 '15 at 13:52
  • just to note: indexOf *will* work if you check a list that contains your object *instance*. E.g.: `a = {}; b=[a]; b.indexOf(a)` works. – bebbi Jun 29 '15 at 13:58
  • @downvoter Why the downvote? –  Jun 29 '15 at 19:53

4 Answers4

1

try this, if all fields are equals return true

function testContains(arrayOfObject, lookingObject )

    var index;

    for (index = 0; index < arrayOfObject.length; ++index) {

        var o = arrayOfObject[index]);

        if(JSON.encode(o)===JSON.encode(lookingObject)) {

            return true;
        }

    }

    return false;
}
Kaiser
  • 748
  • 7
  • 21
  • Heey. Thanks, I optimized your answer a bit, but you got me into the right track using JSON. – andzep Aug 27 '15 at 23:15
0

indexOf() will work only for elementary types like numbers, strings, boolean etc. It does not do deep comparison to match objects.

Something like the following should work:

var found = false;
arr.forEach(function(_object) {
    // code to set found to true if _object is what you need
});
// if found is true, your array has your object, else it doesn't
techfoobar
  • 65,616
  • 14
  • 114
  • 135
  • 1
    You should probably use [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) instead of `forEach`. – Bali Balo Jun 29 '15 at 14:02
  • @BaliBalo - Thank you. Never used `some()` before. But it does looks like a better fit in this case. – techfoobar Jun 29 '15 at 14:04
0

If you can use Lodash (it's really useful and worth it in my opinion) you can use _.find: _.find(collection, object)

or if you want to only know if the object is present in array, you can do: _.includes(collection, object)

Július Retzer
  • 1,055
  • 1
  • 10
  • 25
0

I optimized @Kaiser'S answer into a one-liner:

var found = obj_list.map(JSON.stringify).indexOf(JSON.stringify(obj)) !== -1;

But, this will only work, if the order of the objects is the same:

The JSON represantation of obj1 = {'a': 1, 'b': 2} wouldn't match obj2 = {'b': 2, 'a': 1}

But if the object you're looking for was copied from the list then the test should work.

Also to take into account, this will only work if the objects in the list are simple objects which can be "stringified".

andzep
  • 1,877
  • 24
  • 35