2

I have an array & a literal object like this

var arr = [{a: 1}, {a: 2}, {a: 3}];
var e1 = {a: 1};

I want to implement a function (ex: containsObject(e1, arr)) what should return true in this case.

Please teach me the way without overriding base classes like Array.prototype.indexof

Vu Le Anh
  • 708
  • 2
  • 8
  • 21
  • `containsObject` should be implemented in such a way that you also pass `key` to be tested in `base-array` – Rayon Apr 27 '16 at 11:42
  • http://stackoverflow.com/questions/8217419/how-to-determine-if-javascript-array-contains-object – Karthik N Apr 27 '16 at 11:44
  • http://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects – Bergi Apr 27 '16 at 11:52
  • Related: [How to determine equality for two JavaScript objects?](http://stackoverflow.com/q/201183/218196) – Felix Kling Apr 27 '16 at 11:58

4 Answers4

2

Try this:

var arr = [{a: 1}, {a: 2}, {a: 3}];
var e1 = {a: 1};
for (var i=0; i<arr.length; i++) {
  var found = true;
  for (var key in e1) {
    if (e1[key] != arr[i][key]) {
      found = false;
      break;
    }
  }
  if (found) {
    alert('found at index ' + i);
    break;
  }
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
2
        var arr = [{a: 1}, {a: 2}, {a: 3}];
var e1 = {a: 1};
function containsObject(e1, arr) {
    var i;
    for (i = 0; i < arr.length; i++) {
        if (JSON.stringify(arr[i]) === JSON.stringify(e1)) {
            alert("we found it")
        }else{
            alert(JSON.stringify(e1)+"not equal to"+JSON.stringify(arr[i]))
        }
    }
}
containsObject(e1, arr)

if you don't mind compare them in string, use this way. JSON.stringify() convert object to json string, I think you expect compare them in human vision, then that is it.

I don't think the 2 different object can be compared.they are 2 instance from Object,

Ma Yubo
  • 215
  • 1
  • 10
  • Thank Ma Yubo. We save a loop, but I can trust function JSON.stringify. Is there any case what will be wrong with JSON.stringify? – Vu Le Anh Apr 27 '16 at 12:10
  • don't know yet, JSON.stringify should be able to use for any object – Ma Yubo Apr 27 '16 at 12:21
  • yes, I know function JSON.stringify. I just consider in what case, this way is wrong (not find yet). I prefer your way and use it, the jcubic's way is right. I thought about it, but I am afraid of 2 loop :) Thank again – Vu Le Anh Apr 27 '16 at 12:24
0

This may Help you

function containsObject(e1, arr)
    {
      var k=false;
      ap=arr.filter(function(a){
      if(JSON.stringify(a) === JSON.stringify(e1))
      {k=true; 
       return(true);}
     });
    (k===true)?console.log("in array"):console.log("not in array");
    }
var arr = [{a: 1}, {a: 2}, {a: 3}];
var e1 = {a: 1};
containsObject(e1,arr);

Since array is not containing any method so we can use stringify to compare objects.

Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
-2

This may help you.

function containsObject(e1, arr) {
    var i;
    for (i = 0; i < arr.length; i++) {
        if (arr[i] === e1) {
            return true;
        }
    }

    return false;
}
Deepak Mani
  • 125
  • 11