Skip to content Skip to sidebar Skip to footer

Check If Object Is Already Present In Array?

I'm working with a huge Array of data, for this question I will write something similar avoiding all the key / value. There is an Array of Objects: [ { id: 0, name: 'Tom', age: '

Solution 1:

Using Array.prototype.filter

You can use Array.prototype.filter, as mentioned in this other question.

var people = [
  { id: 0, name: 'Tom', age: '18' },
  { id: 1, name: 'Rob', age: '22' },
  { id: 2, name: 'Carl', age: '19' }
];

functionpersonExists(id){
  return !!people.filter(function(person){
    return person.id == id;
  }).length;
}

document.body.innerHTML = personExists(2) // true
                        + '<br>'
                        + personExists(5); // false

But that method would loop through all items, even if the person is found from the start.

Using a loop

To avoid this, you could use a good old loop:

var people = [
  { id: 0, name: 'Tom', age: '18' },
  { id: 1, name: 'Rob', age: '22' },
  { id: 2, name: 'Carl', age: '19' }
];

functionpersonExists(id){
  for(var i=0; i<people.length; i++){
      if(people[i].id == id) returntrue;
  }
  returnfalse;
}

document.body.innerHTML = personExists(2) // true
                        + '<br>'
                        + personExists(5); // false

Using Object property names as IDs

Another method would enhance performance, but would require you to change your Array into an object:

var people = {
  '0' : { name: 'Tom', age: '18' },
  '1' : { name: 'Rob', age: '22' },
  '2' : { name: 'Carl', age: '19' }
};

functionpersonExists(id){
  return people.hasOwnProperty(id);
}

document.body.innerHTML = personExists(2) // true
                        + '<br>'
                        + personExists(5); // false

Solution 2:

If you don't want to use a for loop and aren't stuck with supporting old browsers (e.g. IE8), you could use Array.prototype.filter to return an array with any users that match the userId you're searching for, e.g.

users.filter(function(user) {
    return user.id === userId; 
});

Then you can test whether or not the returned array is empty.

Solution 3:

If the id is auto generated and not far-off numbers then I would prefer to make array index and id as same. For example,

Object having id as 8 should be on index 8 in the array. Object having id as 10 should be on index 10 in the array.

Hence Object can be picked up by id using the index. But if there are gaps between the ids then this solution will backfire with space complexity.

Solution 4:

To test if a given id is in the array, you can use Array.prototype.some:

var haystack = {/* your object array */},
    // the id to search for:
    needle = 0,

// Array.prototype.some() returns a Boolean true/false// which corresponds to://     true -  one of the object ids is equal to the needle,//     false - none of the object ids are equal to the needle
idIsInArray = haystack.some(function (obj) {
    return obj.id === needle;
});

var haystack = [{
    id: 0,
    name: 'Tom',
    age: '18'
}, {
    id: 1,
    name: 'Rob',
    age: '22'
}, {
    id: 2,
    name: 'Carl',
    age: '19'
}],
    needle = 0,
    idIsInArray = haystack.some(function (obj) {
        return obj.id === needle;
    });

console.log(idIsInArray);

More usefully, though, would be retrieving the index of the object:

var haystack = [/* your array of objects */],
    needle = 2,

    // Array.prototype.map() retains returns a new array,// in this case if the obj.index is equal to the needle// it will contain the index of that object, all other// values will be undefined:
    needleOnly = haystack.map(function (obj, index) {
        if (obj.id === needle) {
            return index;
        }
    }),

// here we get the index of the needle from the needleOnly array// we created, which has the same number of elements as the// haystack array, but contains only the index points of those// array-elements whose id was equal to the needle:
    needleAtIndex = needleOnly.indexOf(needle);

console.log(needleAtIndex, haystack[needleAtIndex]);

var haystack = [{
    id: 0,
    name: 'Tom',
    age: '18'
  }, {
    id: 1,
    name: 'Rob',
    age: '22'
  }, {
    id: 2,
    name: 'Carl',
    age: '19'
  }],
  needle = 2,
  needleOnly = haystack.map(function(obj, index) {
    if (obj.id === needle) {
      return index;
    }
  }),
  needleAtIndex = needleOnly.indexOf(needle);

console.log(needleAtIndex, haystack[needleAtIndex]);

References:

Solution 5:

You could use the lodash library, the include method.

You could also use the grep method :

var new_array = $.grep(old_array, function(e) {
        return e.id == id_of_wanted_object;
    });

if the object is present will have length bigger than zero

Post a Comment for "Check If Object Is Already Present In Array?"