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

7
  • 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 and check if any item matches array2. Commented Nov 30, 2016 at 16:25
  • but var array1 is an associative array (value pare) .... isn't it? Commented Nov 30, 2016 at 16:32
  • 1
    well.... no. To quote: "JavaScript does not support arrays with named indexes. In JavaScript, arrays always use numbered indexes. "* Commented Nov 30, 2016 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. Commented Nov 30, 2016 at 16:36
  • Javascript is weird, is what we're saying Commented Nov 30, 2016 at 16:38

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

4 Comments

I think your answer is the best solution
The answer yuvi provided has a big problem if the sorting is not the same.. An element could be found at position 1 and not in position 0
So how do I return the position of the item found ?
@jamesOduro, it does not matter, because all elements of the array are checked. please see edit with reversed items.
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);

3 Comments

what is the object property...do you mean the 'key'??
object is the same as your array2
@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.
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

3 Comments

Note: It might be a problem if object's attributes are differently sorted.
for some reason to help my project I choose yours as the answer
Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.