I want to combine multiple CSV files into one 2D array using a loop using jquery.
function drawChart(){
filename="/projectUCF/SN00.csv";
$.get(filename, function(csvString){
var arrayData1 = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var removeRows = 1;
while (removeRows--) {
arrayData1.shift();
}
filename="/projectUCF/SN01.csv";
$.get(filename, function(csvString){
var arrayData2 = $.csv.toArrays(csvString, {onParseValue: $.csv.hooks.castToScalar});
var removeRows = 2;
while (removeRows--) {
arrayData2.shift();
}
var arrayData=[...arrayData1,...arrayData2];
console.log(arrayData);
var data = new google.visualization.arrayToDataTable(arrayData);
var view = new google.visualization.DataView(data);
view.setColumns([1,5]);
var options = {
title: "Battery State of Charge (7 days)",
hAxis: {format: 'M/d hh:mm'},
vAxis: {title: "Charge Level (in Percentage)", minValue:0, maxValue:100},
legend: 'none'
};
chart.draw(view,options);
});
});
}
Instead of using $.get multiple times, I want to use a loop. I am new to this, so any help is appreciated.
One way to do this is to use
Promise.all
. It takes an array of promises and calls a single callback when all of them have resolved. So you could restructure the retrieve/parse part of your code like this: