Rodriguez Fernando
Rodriguez Fernando

Reputation: 33

For loop is not working with json data

I'm trying to make a loop in javascript with the following code. It's getting length from json data. The console.log(albums.data.length); line is working and returning 3. Why the loop is not working then? The console.log(x); is not returning anything, even not blank line. There is also no error in console.

function getBestPhoto(albums){
    console.log(albums);
    console.log(albums.data.length);
    for(var x in albums.data.length){
        console.log(x);
        for(var y in albums.data[x].photos.length){
            console.log(y);
        }
    }
}

I have tried another type of loop(for(var i = 0; i < blabla; i++)) but its not working too.

Edit: I wanna use for(var x = 0; x < albums.data.length; x++){ console.log(albums.data[x].photos.id); } instead of

for(var x in albums.data){

How can i do it?

Upvotes: 1

Views: 1560

Answers (2)

Javad
Javad

Reputation: 6016

The for-in loop is not for arrays, it is for iterating over properties/fields of an object. If albums.data is an array, you should use the forEach loop statement instead. If albums.data is an object, and you are trying to access its properties/fields/attributes, you can use the for-in construct.

If albums.data is an array, try:

albums.data.forEach(function(element, index) {
  // Here you have access to each element (object) of the "albums.data" array
  console.log(element.toString());
  // You can also access each element's properties/fields if the element is an 
  // object. 
  // If the element is an array itself, you need to iterate over its elements 
  // in another inner foreach. 
  // Here we are accessing the "photos" property of each element - which itself
  // is another array.
  element.photos.forEach(function(photo, index) {
    // Here you have access to the elements of the "photos" array 
    // Each element of the "photos" array is put in the photo variable. 
    // Assuming each element of the "photos" array is an object, you can access 
    // its properties, using the dot notation:
    console.log("photo id=", photo.id);
    // If the property name (e.g. "id") is not a valid javascript name 
    // (has some special symbols), use the bracket notation
    console.log("photo URL=", photo["photo-url"]);
  });
});

You can also use the lodash library for this (and many other neat functionalities).

If albums.data is an object, try:

for (var prop in albums.data) {
  // Again, this construct is for accessing properties of an object 
  // (e.g. if albums.data is an object), not an array.
  console.log("property name=" + prop + ", " + "property value=" +
              albums.data[prop]);
}

Upvotes: 0

MarsPeople
MarsPeople

Reputation: 1824

You should remove .length from loops

function getBestPhoto(albums){
    console.log(albums);
    console.log(albums.data.length);
    for(var i = 0; i < albums.data.length; i++){
        var x = albums.data[i];
        console.log(x);
        for(var j = 0; j < albums.data[i].photos.length; j++){
            var y = albums.data[i].photos[j];
            console.log(y);
            console.log(albums.data[i].photos[j].id);
        }
    }
}

Upvotes: 1

Related Questions