forum

Home / DeveloperSection / Forums / Get the first value from a json array

Get the first value from a json array

Anonymous User337520-Jun-2013
Hi Everyone,

I have JSON coming from the database that's shaped like this:

var data = {
    "zone01": [{
        "region": "North",
        "state": "New York",
        "county": "Albany",
        "code": "01"
    }, {
        "region": "North",
        "state": "New York",
        "county": "Albany",
        "code": "05"
    }, {
        "region": "North",
        "state": "Maine",
        "county": "Auburn",
        "code": "07"
    }],
    "zone02": [{
        "region": "South",
        "state": "Florida",
        "county": "Gainseville",
        "code": "82"
    }, {
        "region": "South",
        "state": "Florida",
        "county": "Macclenny",
        "code": "73"
    }]
};

I'm trying to get a list of the first set of values "zone01", "zone02" etc... in order to populate a select input. By selecting the zone you'll be able to select down the list but I can't seem to get a list of the zones.

I tried:

$.each(data, function(index, element){
    var zone = JSON.stringify(element.index);
    $('#zone').append('<option value="'+zone+'">'+zone+'</option>');
});

Thanks in advance. 

Updated on 20-Jun-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By