views:

130

answers:

4

I've got a json file included in my javascript that defines a variable that looks like this:

var places_json = { 
 "count": 13476, 
 "places": [
      {"area": "London", "county": "STS", "lat": 52.300151820000004, "lon": -2.36665606, "code": "7567", "id": 1}, 
      {"area": "Sheffield", "county": "STS", "lat": 51.33648680000003, "lon": 0.98861179000000001, "code": "9919", "id": 6}, 
      {"area": "Huxton", "county": "STS", "lat": 53.27483902, "lon": -1.0146250700000001, "code": "9953", "id": 11}, 
 ]}

And I want to retrieve the value of area for the entry whose id is 11, using Javascript.

Does anyone know how to do this? It's too hard for me - my Javascript is not that advanced. If I knew the array index of the entry that I need, then I could do:

var entry = json_places.places[i] 

but I don't know it, unfortunately, just the value of id (which is unique).

Thanks in advance.

+1  A: 

Simply loop through your places to find the proper item.

var area = null;
for(var i = 0; i < json_places.places.length; i++) {
  if(json_places.places[i].id == 11) {
    area = json_places.places[i].area;
    break;
  }
}

alert(area);

If you want a function to do it for you, you can run the following:

json_places.get_place = function(id) {
   return (function(id) {
             for(var i = 0; i < this.places.length; i++) {
               if(this.places[i].id == id) {
                 return this.places[i];
               }
             }

             return null;
           }).apply(json_places, id);
}

Then you can simply do the following:

alert(json_places.get_place(11).area);
Andrew Moore
Use `for in` for hashes, not arrays. It's a bad habit. :)
Anthony Mills
@Anthony Mills: There is nothing wrong with using `for in` for arrays. Section `12.6.4` of `ECMA-262` states that any object whose `[[Enumerable]]` attribute is true can be used in such loop. An array qualifies.
Andrew Moore
AP257
@Andrew, using `for...in` works, yes, but it's dead slow - http://blogs.sun.com/greimer/resource/loop-test.html - and so is generally accepted as a bad practice.
J-P
@Andrew, also, `for...in` exposes anything added to `Array.prototype` or to the `Array` instance.
J-P
@Andrew Moore Using "for in" on array can give you some surprises. You're not sure of the order of the elements(no more sorting). And then you may loop on some useless custom Array extensions usually provided by some JS libs. Ok, a hasOwnProperty check can avoid it, but then why do all this, when a simple i=0, etc... do the job.
Mic
Crockford suggests using hasOwnProperty, which is fair enough. Not doing that check can cause some sloppy code.Chances are if you care about clean, maintainable code with prototypical inheritance that you do this already.
Will Morgan
A: 
function get_area(places_json, id) {
    for (var i = 0; i < places_json.places.length; i++) {
        if (places_json.places[i].id === id) {
            return places_json.places[i].area;
        }
    }
}

Also, if you know you have JavaScript 1.5 available (or you don't mind pasting in the code at https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter ...), and especially if you know LINQ:

function get_area(places_json, id) {
    return places_json.places.filter(function (place) { place.id === id; })[0].area;
}
Anthony Mills
+3  A: 
function findAreaById(placesArray, id) {    
    var place = null;
    for (var i = 0, length = placesArray.length; i < length; i++) {
        place = placesArray[i]; 
        if (place.id === id) {
            return place.area;
        }
    }
    return null;
}
ChaosPandion
A: 

Using jQuery (even though this question has been sufficiently answered):

var area = null;
$.each(places_json.places, function() {
    if(this.id == 11) {
        alert("Area is " + this.area);
        area = this.area;
        return false;
    }
});

See:

http://api.jquery.com/jQuery.each/

karim79