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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -