anil naraindas
anil naraindas

Reputation: 15

Iterate through objects in JSON

I am trying to iterate through the follow json file

[enter image description here]

In order to access the rank of each item in api.standings[0] so I can create a new table cell with the rank key.

This is my my current code:

data.api.standings[0].forEach(elements => {

        var tr = document.createElement('tr');

        tr.innerHTML = '<td>' + data.api.standings[0].rank + '</td>';

        table.appendChild(tr);

    });

However I am getting undefined in the cell.

Upvotes: 1

Views: 37

Answers (1)

KiaiFighter
KiaiFighter

Reputation: 667

Assuming your data is correct, you are iterating through the array standings[0]. In which case, you need to adjust how you reference the objects in the innerHTML assignment statement.

data.api.standings[0].forEach(elements => {

    var tr = document.createElement('tr');

    tr.innerHTML = '<td>' + elements.rank + '</td>';

    table.appendChild(tr);

});

Upvotes: 2

Related Questions