Meek Meek - 15 days ago 9
jQuery Question

Looping through json with multiple arrays

I'm pulling out images from Instagram using json and jQuery.

The json data contains objects and arrays. Somehow I can't loop though the second array on my way down to the values I need.

This is my code:

var request = "./myapi.php?user=jamieoliver"; //&callback=myFunction
$.ajax({
cache: false,
dataType: "json", // or "jsonp" if we enabled it
url: request,
success: function(response) {
console.log(response);
for (var i = 0; i < response.entry_data.ProfilePage.length; i++) {
console.log(response.entry_data.ProfilePage[i].user.media.nodes[i].thumbnail_src);
}
},
error: function(xhr, status, error) {}
});


The problem seens to be here:
nodes[i]
- nodes[] is an array - and my code doesn't loop through it - it only gives me the value inside the first object inside nodes[]. How do I loop through nodes[] in order to get the value of
thumbnail_src
inside each of its objects?

I don't have live data but here's a screenshot of the structure of the json response: enter image description here

Answer

Something like this :

var request = "./myapi.php?user=jamieoliver"; //&callback=myFunction
$.ajax({
  cache: false,
  dataType: "json", // or "jsonp" if we enabled it
  url: request,
  success: function(response) {
    console.log(response);
    for (var i = 0; i < response.entry_data.ProfilePage.length; i++) {
        for (var j = 0; j < response.entry_data.ProfilePage[i].user.media.nodes.length; j++) {
          console.log(response.entry_data.ProfilePage[i].user.media.nodes[j].thumbnail_src);
       }
    }
  },
  error: function(xhr, status, error) {}
});