javascript - Access the nested value in JSON without looping through it -
this json, want directly zipcodes values json without looping through json. how can it?
countries:[ { name:'india', states:[{ name:'orissa', cities:[{ name:'sambalpur', zipcodes:{'768019','768020'} }] }] } ]
as not associative array, option use indexes this:
countries[x].states[y].cities[0].zipcodes
where x
each representation of state in array, in case, of course, have more one.
similarly y
each state in each state in each country, in case have more of , can same cities if need to.
edit: here's how can iterate array:
for(var c in countries) { var name = countries[c].name; if (name === "countryiamlookingfor") { var stateslist = countries[c].states; (var s in stateslist) { var statename = stateslist[s].name; ..... } } }
you can keep iterating until find country, state, , city need, extract zipcodes
there shown in previous code snippet.
Comments
Post a Comment