up vote 0 down vote favorite
share [g+] share [fb]

Possible Duplicate:
Find object by id in array of javascript objects

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". Basically, multiple data points are stored for each person.

What I'm trying to do is 'if id "820" is found in ArrayofPeople, true else false.

How would this be done?

link|improve this question

78% accept rate
feedback

closed as exact duplicate by Felix Kling, hvgotcodes, Robert Harvey Oct 12 '11 at 15:17

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

3 Answers

something like this:

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

var result = in_array(ArrayofPeople, 235);
link|improve this answer
+1, maybe use the strict === instead though. – Joe Tuskan Oct 12 '11 at 12:51
thnx, i've added the === operator – Manuel van Rijn Oct 12 '11 at 12:52
This seems to work for me if it's false, but if it's true it stops working altogether. What am I doing wrong in this check?: if (in_array(ArrayofPeople, 235) == true) { alert("event was in array"); } else { alert("not in array"); } – Oseer Oct 12 '11 at 13:09
because you store the id's as a string you should doe in_array(arrayofpeople, '235') or you need to remove the "" from the id property in the array – Manuel van Rijn Oct 12 '11 at 13:14
feedback

you should iterate over the array and manually check if you have a matching id :

function getPersonById(id){
    for(var i=0,l=ArrayofPeople.length;i<l;i++)
       if(ArrayofPeople[0][i].id == id)
           return ArrayofPeople[i];
    return null;
}

Of course, this is pretty inefficient. I suggest you store your objects into an associative array (a.k.a. an object) indexed by the persons id. Then, the access to a person with a certain id is immediate since object are nothing than hash-tables :

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

 function getPersonById(id){
   return id in ArrayofPeople 
       ? ArrayofPeople[id]
       : null;
}
link|improve this answer
feedback
ArrayofPeople = new Array();
ArrayofPeople[0] = [{"id": "529", "name": "Bob"}];
ArrayofPeople[1] = [{"id": "820", "name": "Dave"}];
ArrayofPeople[2] = [{"id": "235", "name": "John"}];

var str = '820';
var is_found = 'not found';
for(item in ArrayofPeople){
    target = ArrayofPeople[item][0];
    if(target['id'] === str)
        is_found = 'found';
}
alert(is_found);
link|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.