Andy Anderson
Andy Anderson

Reputation: 25

How to make a for loop to grab a data from JSON array

I would like to grab a data from JSON array, which looks like below, and so this.responceText returns this data. And I try to use the data from my Javascript code, but it is not working, and also there is no error message. Where is wrong in my code? Thanks.

{"0":{"folder":"callofthewild","title":"Call of the Wild"},"1":{"folder":"2001spaceodyssey","title":"2001: A Space Odyssey "},"2":{"folder":"hobbit","title":"The Hobbit"},"4":{"folder":"pokemon","title":"I Choose You! (Pokemon Chapter Books)"},"5":{"folder":"alannathefirstadventure","title":"Alanna: The First Adventure (Song of the Lioness #1)"}}

part of my javascript;

var books = JSON.parse(this.responseText);

for (var i = 0; i < books.length; i++) {
    var book = document.createElement("div");
    var text = document.createElement("p");
    var image = document.createElement("img");
    image.src = "books/" + books.i.folder + "/cover.jpg";
    text.innerHTML = books.i.title;

    book.appendChild(image);
    book.appendChild(text);
    document.getElementById("allbooks").appendChild(book);
}

Upvotes: 0

Views: 82

Answers (4)

Sivasankar
Sivasankar

Reputation: 803

You are using JSON Object and not an array. And 'length' is not a property for Array.

So it iterate over this, you can use:

for (var bookKey in books) {
    var book = books[bookKey];
    // And create html content here
}

Upvotes: 0

A Paul
A Paul

Reputation: 8251

Your json is not an array.. so your .length will be undefined

$.each(books, function(i, n) {
    var book = document.createElement("div");
    var text = document.createElement("p");
    var image = document.createElement("img");
    alert(books[""+i+""].folder)
    image.src = "books/" + n.folder + "/cover.jpg";
    text.innerHTML = n.title;

    book.appendChild(image);
    book.appendChild(text);
    document.getElementById("allbooks").appendChild(book);
});

Upvotes: 1

Pedro Rodrigues
Pedro Rodrigues

Reputation: 1678

A literal object hasn't length property.

var books = {
  "0": {
    "folder": "callofthewild",
    "title": "Call of the Wild"
  },
  "1": {
    "folder": "2001spaceodyssey",
    "title": "2001: A Space Odyssey "
  },
  "2": {
    "folder": "hobbit",
    "title": "The Hobbit"
  },
  "4": {
    "folder": "pokemon",
    "title": "I Choose You! (Pokemon Chapter Books)"
  },
  "5": {
    "folder": "alannathefirstadventure",
    "title": "Alanna: The First Adventure (Song of the Lioness #1)"
  }
};

Object.keys(books)
    .map(key => books[key])
    .forEach(book => {
        var div = document.createElement("div");
        var text = document.createElement("p");
        var image = document.createElement("img");
    
        image.src = "books/" + book.folder + "/cover.jpg";
        text.innerHTML = book.title;

        div.appendChild(image);
        div.appendChild(text);
    
        document.getElementById("allbooks").appendChild(div);
});
<div id="allbooks"></div>

Upvotes: 0

Matias Cicero
Matias Cicero

Reputation: 26281

Since your JSON is an object (not an array), you can use Object.keys() to get all its keys and then iterate over its keys to get the appropiate values:

var books = JSON.parse(this.responseText);
Object.keys(books).forEach(function (index) {
   var book = books[index];
   var div = document.createElement("div");
   var text = document.createElement("p");
   var image = document.createElement("img");
   image.src = 'books/' + book.folder + '/cover.jpg';
   text.innerHTML = book.title;
   div.appendChild(image);
   div.appendChild(text);
   document.getElementById('allbooks').appendChild(div);
});

Upvotes: 2

Related Questions