0

i try to add dataset to chartjs using a for loop but it only add the first element. https://jsfiddle.net/sp1hkb3r/

for (i = 0; i < performances.length; i++) {
    console.log(i)
    console.log(performances.length)
    myChart.data.datasets.push(getDataset(performances[i]));
    myChart.update();
}

return 0 3 in console, and chartjs's datasets doesn't have performances[1] and performances[2] data

2 Answers2

0

I believe it's a simple matter of scope. Try declaring your loop variable as let i instead of just i.

Ayrton
  • 2,075
  • 1
  • 10
  • 24
0

Exactly, just declare the variable i using let. Check out this post javascript-variable-declaration-within-loop