14

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oseer
  • 740
  • 7
  • 19
  • 28

5 Answers5

11

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manuel van Rijn
  • 10,170
  • 1
  • 29
  • 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
  • 3
    Wow, almost 4 years and nobody mentioned that this code is not working! The code will check only if the first element's id equals to the id param! change the body of the inner loop to `if(array[i][0].id === id) return true;` – A.Akram Jan 18 '16 at 10:08
8

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[i][0].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 person's id. Then, the access to a person with a certain id is immediate since objects 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;
}
gion_13
  • 41,171
  • 10
  • 96
  • 108
4

You can use the relatively new Array.prototype.some() to find whether an item exists (a shim is provided in the documentation):

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

function in_array(array, id) 
{
    return array.some(function(item) {
        return item[0].id === id;
    });
}

console.log(in_array(ArrayofPeople, '820')); // true
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1
function IsIdInArray(array, id) {
  for (var i = 0; i < array.length; i++) {
    if (array[i].id === id)
      return true;
  }
  return false;
}

var result = IsIdInArray(ArrayofPeople, 820);
showdev
  • 28,454
  • 37
  • 55
  • 73
Ehasanul Hoque
  • 578
  • 8
  • 14
1
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);
Riz
  • 9,703
  • 8
  • 38
  • 54