I'm trying to create many charts each with different JSON data. I have a loop which tries to load the data for each chart, but although the calls are asynchronous, the loop index is always the last one. Instead of doing 0,1,2,3,4,5 it does 5,5,5,5,5,5. Here's an example with 2 items to loop over:
var charts_data = [ [some_url, "value1", "value2", 1],
[some_url, "value1", "value2", 1] ]
for (var i = 0; i < charts_data.length; i++) {
var _this = this;
var _i = i
d3.json(charts_data[i][0], function(error, json_data) {
console.log("- - - - - - - - - - - - - - - - - - #"+ _i +" ORIGINAL DATA : ")
format_raw_data(json_data, _this.charts_data[_i][1], _this.charts_data[_i][2], _this.charts_data[_i][3]);
});
}
And the problem is shown by the console log which says:
- - - - - - - - - - - - - - - - - - #1 ORIGINAL DATA :
- - - - - - - - - - - - - - - - - - #1 ORIGINAL DATA :
I tought that d3.json being asynchrnous meant this wouldn't happen, but I guess I don't fully grasp that concept.
Any ideas for a remedy?