Cherry Cherry - 6 months ago 31
jQuery Question

How to know whether the object contains in my Array List of jquery?

In my Ajax success function data, I am getting a list of objects. I have one more array list with objects which are of same type. How to write a condition to check if that object contains in my list?

//In my Ajax Success function
$.each(data, function(index, item) {
//This is my another array list
var target = [];
target = self.target();
//Need to check the condition whether the above "item" is in my above array list


I am doing something like this, its not working

if (target.indexOf(item) !== -1) {
//item is not present in the above target array list
console.log(item);
} else {
console.log(item.FullName);
}

Answer

From How to determine if object is in array

function containsObject(obj, list) {
var i;
for (i = 0; i < list.length; i++) {
    if (list[i].YourId === obj.Id) {
        return true;
    }
}

return false;
}