1

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!

Ron_LHY
  • 11
  • 2
  • Return where exactly, the `onload()` function is asynchronous, you can't just return from it ? – adeneo Jun 28 '16 at 21:04
  • you could move the path0 variable outside the function. The reason you are getting the error is because you are either trying to use the path0 before it is set or because wherever you are calling it, the variable is not being recognized. – applecrusher Jun 29 '16 at 01:45

0 Answers0