2

How do I find Array2 in Array1 . I have been using $.inArray() method or indexOf() but it return false yet the statement is true.

var array1 = [{
    h: 1480508328,
    rid: 16,
    sid: 2
  }, {
    h: 87542,
    rid: 18,
    sid: 9
  }
];
var array2 = {
  h: 1480508328,
  rid: 16,
  sid: 2
};
//test if array2 exist in array1
if ($.inArray(array2, array1) > 0) {
  console.log('Object is in array');
} else {
  console.log('Object is not in array');
}

Please help. Thank you

kukkuz
  • 41,512
  • 6
  • 59
  • 95
james Oduro
  • 673
  • 1
  • 6
  • 22
  • 5
    FYI, `array2` is not an array. It's an object. `array2` is also not in `array1`, only an object which *looks* like `array2`. So you'll have to [iterate over `array1`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for) and check if any item matches `array2`. – Mike Cluck Nov 30 '16 at 16:25
  • but var array1 is an associative array (value pare) .... isn't it? – james Oduro Nov 30 '16 at 16:32
  • 1
    well.... no. [To quote](http://www.w3schools.com/js/js_arrays.asp): "JavaScript does not support arrays with named indexes. In JavaScript, arrays always use numbered indexes. "* – yuvi Nov 30 '16 at 16:34
  • 1
    @jamesOduro In a way you could say that but we don't call them associative arrays in JS since there's no guarantees on order. They're called objects. – Mike Cluck Nov 30 '16 at 16:36
  • Javascript is weird, is what we're saying – yuvi Nov 30 '16 at 16:38
  • okay... I guess am bring the idea of PHP ASSOCIATIVE ARRAYS. I always assume javascript object as associative array since they are value/pair ....anyway thanks for explanation – james Oduro Nov 30 '16 at 16:40
  • Well, funny you should mention that. See, in PHP, associative arrays aren't that either. They're actually [ordered maps optimized to be used as arrays](http://stackoverflow.com/a/10914745/2387772). – yuvi Nov 30 '16 at 16:51

3 Answers3

4

You could iterate the array and check the length of the properties with the length of the given object and check every value.

var array = [{ h: 1480508328, rid: 16, sid: 2 }, { h: 87542, rid: 18, sid: 9 }],
    object = { h: 1480508328, rid: 16, sid: 2 },
    found = array.some(function (a) {
        var keys = Object.keys(a);
        return keys.length === Object.keys(object).length && keys.every(function (k) {
            return a[k] === object[k];
        });
    });
  
console.log(found);

var array = [{ h: 87542, rid: 18, sid: 9 }, { h: 1480508328, rid: 16, sid: 2 }],
    object = { h: 1480508328, rid: 16, sid: 2 },
    found = array.some(function (a) {
        var keys = Object.keys(a);
        return keys.length === Object.keys(object).length && keys.every(function (k) {
            return a[k] === object[k];
        });
    });
  
console.log(found);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

Use Array.prototype.every to check if each object property of the array and the object matches - now use Array.prototype.some to check if at least once the object exists inside the array.

See demo below:

var array1=[{h:1480508328,rid:16,sid:2},{h:87542,rid:18,sid:9}];
var object={h:1480508328,rid:16,sid:2}

var result = array1.some(function(e){
  return Object.keys(object).length === Object.keys(e).length && Object.keys(e).every(function(k){
    return k in object && e[k] === object[k];
  });
});

console.log(result);
kukkuz
  • 41,512
  • 6
  • 59
  • 95
  • what is the object property...do you mean the 'key'?? – james Oduro Nov 30 '16 at 16:37
  • `object` is the same as your `array2` – kukkuz Nov 30 '16 at 16:38
  • @james Oduro Objects have properties and arrays have keys which are also properties since an array is an object in JS. Yet arrays may also have properties which are not keys. It's best to think it like that. – Redu Nov 30 '16 at 16:40
1

You can stringify it and check for equality

var array1 = [{
    h: 1480508328,
    rid: 16,
    sid: 2
  }, {
    h: 87542,
    rid: 18,
    sid: 9
  }
];
var array2 = {
  h: 1480508328,
  rid: 16,
  sid: 2
};

var dat1 = JSON.stringify(array2);
array1.forEach(function(item){
    if(JSON.stringify(item) === dat1){
        console.log(dat1)
    } else {
        console.log('Not match')
    }
});

JSFIDDLE

yuvi
  • 18,155
  • 8
  • 56
  • 93
brk
  • 48,835
  • 10
  • 56
  • 78