-1

I have a huge .csv file. It has about 130,000 rows and 13 columns. I need to create a bunch of graphs using that data in a web app. I am very new to JavaScript. Can anyone help me reading this huge file and store the data into a 2D array?

3 Answers3

0

I think you can use this lib http://c2fo.io/fast-csv or https://www.papaparse.com/

this libs use streams, and you can use huge file and modify each line

0

If you are working on a node app, try looking for packages on npm,

https://www.npmjs.com/search?q=csv

Else, you might want to take a look at the following link for core javascript code:

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

Abhishek Kumawat
  • 780
  • 1
  • 10
  • 25
0

This code will help you:

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

// Reading csv data from file using get ou post or whateven protocol
$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "data.txt",
        dataType: "text",
        success: function(data) {
            var lines = processData(data);
            console.log(lines);
        }
    });
});

// Passing csv data direct from text string
csv = "heading1,heading2,heading3,heading4,heading5\nvalue1_1,value2_1,value3_1,value4_1,value5_1\nvalue1_2,value2_2,value3_2,value4_2,value5_2";
var lines = processData(data);
console.log(lines);


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(data[j]);
            }
            lines.push(tarr);
        }
    }
    return lines;
}
 </head>
khalid
  • 121
  • 8