0

I have this below type of array. I want to iterate this array in JavaScript. How is this possible?

{
"result": true,
"data": [
{
    "ID": 1,
    "stage_ID": 1,
    "badge_type_ID": 1,
    "name": "Despertar da Força",
    "description": "Fazer a primeira SuperAtividade",
    "type": "",
    "image_static": "url123.jpg",
    "image_animated": "",
    "SQL": "",
    "published": 1
},
{
    "ID": 2,
    "stage_ID": 1,
    "badge_type_ID": 1,
    "name": "Super 3",
    "description": "Fazer 3 SuperAtividades",
    "type": "",
    "image_static": "urlimage123.png",
    "image_animated": "",
    "SQL": "",
    "published": 1
}

etc

I tried the following script and it is returning "undefined", "undefined".

$.getJSON('https://www.legiaodossuperpoderes.com.br/chronus/api/adm/badges', function(data) {
        var output= "<ol>";
        for (var i in data) {
            output += "<li>" + data[i].ID + "</li>"
        }
        output+="</ol>";
        document.getElementById("placeholder").innerHTML=output;
    }); 

Any solutions ? Thanks!

4
  • i suppose, that function(data) contains the whole object. you need a reference to data of the object. Commented May 13, 2016 at 19:12
  • try console.log(data) to double check data return from getJSON Commented May 13, 2016 at 19:12
  • try data=data.data. And data[i] is properly a mistake because i is the value of the array not index. Commented May 13, 2016 at 20:34
  • @gdlmx i is not value of array it is index. if u use for(var i of data) mean it will be value of array Commented Dec 1, 2016 at 5:05

2 Answers 2

2

You can try converting to Json Object

$.getJSON('https://www.legiaodossuperpoderes.com.br/chronus/api/adm/badges', function(data) {
        var output= "<ol>";
        var jsondata = $.JSON.parse(data); // Json object Convertion
        for (var i in data) {
            output += "<li>" + data[i].ID + "</li>"
        }
        output+="</ol>";
        document.getElementById("placeholder").innerHTML=output;
    });
Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to access an undefined property using data[i].ID .

When you are getting response from your getJSON() function then you need to call the response with a name and then you can access any inner property using this name .

Now if you want to iterate using data key/value on myResponse variable then you need to do this.

$.getJSON('https://www.legiaodossuperpoderes.com.br/chronus/api/adm/badges', function(myResponse) {
    var output= "<ol>";
    var dataCount = myResponse.data.length;
    for (var i = 0; i < dataCount; i++) {
        output += "<li>" + myResponse.data[i].ID + "</li>";
    }
    output+="</ol>";
    document.getElementById("placeholder").innerHTML=output;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.