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.

3
  • Are you using any JS library? Commented Feb 3, 2010 at 22:29
  • No - just javascript (though I can add one if it's impossible to do it any other way) Commented Feb 3, 2010 at 22:31
  • It's definitely possible, as you can see in the answers below. I was just asking so that I could provide an answer that uses the library instead of generic JS Commented Feb 3, 2010 at 22:36

4 Answers 4

3
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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);

7 Comments

Use for in for hashes, not arrays. It's a bad habit. :)
@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.
Cheers everyone for all the answers. Picking this one because it worked perfectly first time & for the combination of inline code & function. Thanks!
@Andrew, using for...in works, yes, but it's dead slow - blogs.sun.com/greimer/resource/loop-test.html - and so is generally accepted as a bad practice.
@Andrew, also, for...in exposes anything added to Array.prototype or to the Array instance.
|
0
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;
}

Comments

0

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.