I have the following JSON data and I want to populate an html select element from it, please I don't see how to use a for each loop with such a json data. Thanks.
//JSON
[{
"group": "US (Common)",
"zones": [{
"value": "America/Puerto_Rico",
"name": "Puerto Rico (Atlantic)"
}, {
"value": "Pacific/Honolulu",
"name": "Honolulu (Hawaii)"
}]
}, {
"group": "Africa",
"zones": [{
"value": "Africa/Tunis",
"name": "Tunis"
}, {
"value": "Africa/Windhoek",
"name": "Windhoek"
}]
}]
and the html select element looks like :
<select id="timeZones"></select>
and this is JS code that is not working :
var $select = $('#timeZones');
$.ajax({
url: 'timezones.json',
dataType: 'JSON',
success: function (data) {
$.each(data, function (i, val) {
$select.append('<OPTGROUP LABEL="' + val[i].group + '"><OPTION VALUE="' + val[i].zone[i].value + '">' + val[i].zone[i].name + '</OPTION></OPTGROUP>');
})
},
error: function () {
alert("JSON ERROR");
}
});