How to read data From *.CSV file using javascript?

后端 未结 13 807
不知归路
不知归路 2020-11-22 00:46

My csv data looks like this:

heading1,heading2,heading3,heading4,heading5,value1_1,value2_1,value3_1,value4_1,value5_1,value1_2,value2_2,value3_2,val

13条回答
  •  走了就别回头了
    2020-11-22 01:25

    Here is another way to read an external CSV into Javascript (using jQuery).

    It's a little bit more long winded, but I feel by reading the data into arrays you can exactly follow the process and makes for easy troubleshooting.

    Might help someone else.

    The data file example:

    Time,data1,data2,data2
    08/11/2015 07:30:16,602,0.009,321
    

    And here is the code:

    $(document).ready(function() {
     // AJAX in the data file
        $.ajax({
            type: "GET",
            url: "data.csv",
            dataType: "text",
            success: function(data) {processData(data);}
            });
    
        // Let's process the data from the data file
        function processData(data) {
            var lines = data.split(/\r\n|\n/);
    
            //Set up the data arrays
            var time = [];
            var data1 = [];
            var data2 = [];
            var data3 = [];
    
            var headings = lines[0].split(','); // Splice up the first row to get the headings
    
            for (var j=1; j

    Hope this helps someone in the future!

提交回复
热议问题