My code is basically trying to read data from a .csv file and return the file data into an array. Below is the $("#filename").change() function part:
$("#filename").change(function(e) {
var ext = $("input#filename").val().split(".").pop().toLowerCase();
if($.inArray(ext, ["csv"]) == -1) {
alert('Upload CSV');
return false;
}
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function(e) {
var csvval=e.target.result.split("\n");
var path0 = [];
for (var j=0; j<csvval.length; j++) {
var csvvalue=csvval[j].split(",");
if (csvvalue[1] === "TN04G0628" && csvvalue[2] !== "0") {
var ptr = {lat:parseFloat(csvvalue[2]), lng:parseFloat(csvvalue[3])};
path0.push(ptr);
}
}
for (var p=0; p<path0.length; p++) {
console.log(path0[p]);
}
};
reader.readAsText(e.target.files.item(0));
}
return false;
});
My question is how can I return the path0 array at the end of this function. I tried a lot of locations to put the return path0
in the function. However, all of those trials either return an empty array or an undefined array. I feel like this is a relatively basic question but very difficult to me cuz I am new to JS.
Thanks in advance!