I'm trying to use JQuery .get() method and a javascript for loop to process some data from an external file. I've read about closures and return values from callbacks on stackoverflow for a couple hours, and i'm still confused why this isn't working.
Are the variables headers and countryData not global in scope with respect to the inner callback function? They are being assigned values as intended within the callback function, but then how do i access them once that is complete? And possibly an example wihtout using the alert() function?
function processData(inCSV){
var headers;
var countryData = [];
$.get(inCSV, function(data) {
var lines = data.split('\r\n');
for(var i=0;i<=lines.length-1;i++){
var lineData = lines[i].split(',');
if(i != 0){
countryData[lineData[1]] = lineData.slice(2,lineData.length);
} else {
headers = lineData.slice(2,lineData.length);
}
}
console.log('inside',headers); // output 'inside ["1971", "1972", "1973" ...'
console.log('inside',countryData['Brazil']); // output 'inside ["56.4", "54.6", ..'
});
console.log('outside',headers); // output 'outside undefined' ...!?
console.log('inside',countryData['Brazil']); // output 'outside undefined' ...!?
}