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

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -