Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 6 down vote accepted

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);
share|improve this answer
    
+1, maybe use the strict === instead though. –  Joe 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

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;
}
share|improve this answer
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);
share|improve this answer

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