Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  105] [ 6]  / answers: 1 / hits: 21519  / 12 Years ago, thu, november 29, 2012, 12:00:00

How would I check in my array of objects, if a specific item exists (in my case MachineId with id 2)?



[{MachineID:1,SiteID:20},{MachineID:2,SiteID:20},{MachineID:3,SiteID:20},{MachineID:4,SiteID:20}]


I tried this:



if (index instanceof machineIds.MachineID) {
alert('value is Array!');
} else {
alert('Not an array');
}

More From » jquery

 Answers
12

In cross browser way you may use jQuery.grep() method for it:



var item = $.grep(machineIds, function(item) {
return item.MachineID == index;
});

if (item.length) {
alert(value is Array!);
}

[#81720] Wednesday, November 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nolancampbellc

Total Points: 9
Total Questions: 102
Total Answers: 101

Location: Saint Vincent and the Grenadines
Member since Mon, Jan 16, 2023
1 Year ago
;