Oscar Arango Oscar Arango -4 years ago 97
Ajax Question

Javascript for loop with array

Trying to fetch and display data from an array in a for loop but it's only showing one entry. I'm executing this in a trigger for Storyline and when viewing Storyline in the browser I don't get any help from the inspector.

function showInfo(data) {
for (var i = 0; i < data.length; i++) {
var player = GetPlayer();
player.SetVar("name", data[0].name + "\n");
}
}


Seems like something simple to do but not sure whats going on. Perhaps Articulate Storyline adds an extra layer of issues not forseen.

Answer Source

but it's only showing one entry

this is because you're accessing at 0 index for each. You need to use i to correctly iterate over every element.

see:

function showInfo(data) {
    for (var i = 0; i < data.length; i++) {
        var player = GetPlayer();
        player.SetVar("name", data[i].name + "\n");
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download