Monday, 9 September 2019

Check if an object exists in a JavaScript array using jQuery


Check if an object exists in a JavaScript array using jQuery

If you would like to find an object in existing JavaScript array, below is the sample example.

var list= [];

var obj1 = {id:1,name:'paramesh'};

var exist=$.grep(list, function(obj) {
  return obj.name === 'john'; 
});

if(exist.length===0){
  list.push(obj1);
}

No comments:

Post a Comment