I'm trying to implement a function which would load a server-stored CSV file, and parse it into an array. I have some issues with it and would be grateful for help.
Here's how the function looks like at the moment:
function getSeries(filename){
//create an array where objects will be stored.
var seriesInFile=[];
$.get('/public/csv/'+filename+'.csv',
function(data) {
// Split the lines
var lines = data.split('\n');
$.each(lines, function(lineNo, line) {
var items = line.split(',');
//elements from each line are taken and stored
series = {
//some other attributes here, I took them off for this example
data: []
}
$.each(items, function(itemNo, item) {
series.data.push(parseFloat(item));
});
seriesInFile.push(series);
});
console.log(seriesInFile); //it works here!
});
return seriesInFile;
};
console.log(getSeries('hr'));
console.log(getSeries('hr'));
So, the problem is that even though I successfully get information from the CSV file and store it within 'seriesInFile' within the $.each function, as soon as I get out of it it doesn't work anymore - whole function returns an empty array. I guess it might have something to do with the get() function being deployed a bit later than the original getSeries one. Is there a simple solution for it?
Thanks,