Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  28] [ 4]  / answers: 1 / hits: 62310  / 13 Years ago, wed, october 12, 2011, 12:00:00

I have a JavaScript array, where each new item added to the array gets the next incremental number. An example would be as follows (I hope Im writing this correctly):



ArrayofPeople[0] = [{id: 529, name: Bob}];
ArrayofPeople[1] = [{id: 820, name: Dave}];
ArrayofPeople[2] = [{id: 235, name: John}];


The array is named ArrayofPeople, storing multiple data points for each person.



I need to know if an element with id of 820 exists in the array or not. How would this be done?


More From » arrays

 Answers
3

Something like this:



function in_array(array, id) {
for(var i=0;i<array.length;i++) {
return (array[i][0].id === id)
}
return false;
}

var result = in_array(ArrayofPeople, 235);

[#89661] Monday, October 10, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mira

Total Points: 460
Total Questions: 108
Total Answers: 99

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
mira questions
;