jquery - Access to Json array with for function -
how can loop throught array below structure function for?
my array is:
{ "cod": "200", "message": 0.0027, "city": { "id": 2264456, "name": "portimao", "coord": { "lon": -8.53775, "lat": 37.138561 }, "country": "pt", "population": 0 }, "cnt": 1, "list": [{ "dt": 1400328000, "temp": { "day": 24.54, "min": 15.3, "max": 24.69, "night": 15.3, "eve": 23.33, "morn": 23.33 }, "pressure": 1010.53, "humidity": 64, "weather": [{ "id": 800, "main": "clear", "description": "sky clear", "icon": "01d" }], "speed": 5.17, "deg": 150, "clouds": 0 }] }
my idea array values accessible doing this:
for(var = 1; < data.length; i++) { data.list[i].temp.max ; data.list[i].weather[i]icon }
the thing is... i´m not getting information inside weather object... it´s undifined!!! why?
that's because you'll need 2 nested for-loops. also, note indices begin @ zero, not one
for(var = 0; < data.length; i++) { //use: data.list[i].temp.max; (var j=0; j< data.list[i].weather.length; j++){ //use: data.list[i].weather[j].icon } }
easier iterating approach use $.each
or native array.foreach
.
something like:
data.list.foreach(function (listitem, index) { //use: listitem.temp.max listitem.weather.foreach( weatheritem, index) { //use: weatheritem.icon }); });
Comments
Post a Comment