0

I am trying to extract data from a CSV with JavaScript and then put these values into an array and display it. My csv will have multiple rows (5 or more). So far i am trying to use a jquery library to help extract the data. I couldn't get the script to display anything when I ran it. I am new to Javascript so wasn't sure how to do this and how to take the extracted data and put it into an array.

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

<title> Testing Orders</title>
   </head>   <body>


<script language="javascript" type="text/javascript">



$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "tester excel.csv",
        dataType: "text",
        success: function(data) {processData(data);}
     });
});

function processData(allText) {
    var allTextLines = allText.split(/\r\n|\n/);
    var headers = allTextLines[0].split(',');
    var lines = [];

    for (var i=1; i<allTextLines.length; i++) {
        var data = allTextLines[i].split(',');
        if (data.length === headers.length) {

            var tarr = [];
            for (var j=0; j<headers.length; j++) {
                tarr.push(headers[j]+":"+data[j]);
            }
            lines.push(tarr);
        }
    }
    // alert(lines);
   // console.log("tester excel.txt");

}
function getData() {
    if (data == ""){
        return 'DataNotReady';
    }else {

    }

}
 windows.prompt(processData);
</script>


</body>

</html>`
Maduranga E
  • 1,679
  • 5
  • 23
  • 37
val
  • 11
  • 4
  • `url: "tester excel.csv",` this not correct, – Taki Apr 04 '18 at 19:32
  • Can you provide the file or some sample data of how file is structured? – Stivan Apr 04 '18 at 19:36
  • Possible duplicate of [How to read data From \*.CSV file using javascript?](https://stackoverflow.com/questions/7431268/how-to-read-data-from-csv-file-using-javascript) – Banzay Apr 04 '18 at 19:38
  • The csv file will look something like: Data, Sold to Date, L Term, etc. and then the corresponding data underneath them so Data will have the values (8, 12, 34). – val Apr 04 '18 at 19:38

0 Answers0