2

I've been given a bunch of data in CSV form, which needs to be presented visually and audibly. The issue I'm having is I can't figure out how to turn individual lines in the CSV file into different arrays. For example, here is a page of CSV data I have:

Day 5 (08/07/2019),Day 9 (12/07/2019),Day 12 (15/07/2019),Day 16 (19/07/2019),Day 19 (22/07/2019),Day 23 (26/07/2019),Day 26 (29/07/2019),Day 30 (02/08/2019),Day 35 (07/08/2019),
COM 1 A,3,7,16,22,22,31,32,35,44,
COM 1 B,3,5,15,23,23,35,37,37,42,
COM 2 A,2,2,8,21,23,28,34,34,36,
COM 2 B,2,3,11,21,25,26,26,26,33,
COM 3 A,4,9,15,23,23,22,29,36,41,
COM 3 B,3,3,10,15,16,19,21,21,22,
COM 4 A,2,6,11,22,25,32,33,35,41,
COM 4 B,3,4,11,21,22,34,35,36,42,
COM 5 A,1,4,14,20,22,22,22,22,22,
COM 5 B,1,3,8,16,20,28,31,32,35,
COM 6 A,3,3,13,24,28,32,35,35,37,
COM 6 B,2,6,13,22,26,29,35,35,45,
COM AVERAGE,2.417,4.583,12.083,20.833,22.9167,28.167,30.833,32.000,36.667,
COC 1 A,3,5,17,26,28,33,35,35,43,
COC 1 B,2,8,15,18,24,29,35,35,41,
COC 2 A,3,4,17,24,28,36,36,41,46,
COC 2 B,3,3,13,23,27,33,38,38,45,
COC 3 A,5,13,17,22,26,34,37,37,46,
COC 3 B,3,11,15,24,28,36,38,38,45,
COC 4 A,3,6,16,24,27,32,34,35,45,
COC 4 B,2,5,14,20,24,28,30,34,40,
COC 5 A,2,6,13,20,25,28,30,31,41,
COC 5 B,2,2,2,17,22,24,24,24,24,
COC 6 A,2,3,12,19,25,32,32,34,40,
COC 6 B,3,4,6,16,21,23,23,23,26,
COC AVERAGE,2.750,5.833,13.083,21.083,25.4167,30.667,32.667,33.750,40.167,
COB 1 A,4,7,14,19,26,26,28,29,38,
COB 1 B,3,6,11,19,22,22,22,26,32,
COB 2 A,3,5,14,24,26,33,33,39,41,
COB 2 B,2,5,14,21,22,27,30,30,38,
COB 3 A,3,3,11,24,26,33,34,42,42,
COB 3 B,3,5,12,26,26,34,35,39,39,
COB 4 A,3,6,16,21,28,33,36,39,49,
COB 4 B,3,11,15,23,25,32,34,34,44,
COB 5 A,3,5,10,23,25,25,29,31,40,
COB 5 B,1,3,3,18,19,24,25,25,25,
COB 6 A,3,3,6,20,22,33,33,34,39,
COB 6 B,4,7,14,23,26,34,35,35,42,
COB AVERAGE,2.917,5.500,11.667,21.750,24.4167,29.667,31.167,33.583,39.083,

I want to have an array for each line, for example, one for the line starting COM 1 A, another for COM 1 B etc. etc...

I've tried using jQuery-3.4.1.min.js, along with jQuery.csv.js, but I'm not having much luck! The documentation for jQuery.csv.js went over my head, and I tried this...:

FruitNumCOM1A = $.csv.toArray(/*Line number in CSV file*/)

But that obviously doesn't work!

I'm quite new to Javascript so any help would be great!

Saltpot64
  • 21
  • 1
  • Have you tried using the second method `$.csv.toArrays()` as listed under Methods here [https://github.com/typeiii/jquery-csv](https://github.com/typeiii/jquery-csv) ? – mark_b Oct 02 '19 at 15:39

1 Answers1

1

take a look at Reading line-by-line file in JavaScript on client side

$("#files").on('change', function(evt){
// creating FileReader
var reader = new FileReader();

// assigning handler
reader.onloadend = function(evt) {      
    lines = evt.target.result.split(/\r?\n/);

    lines.forEach(function (line) {

        parseLine(...); 

    }); 
};

// getting File instance
var file = evt.target.files[0];

// start reading
reader.readAsText(file);

}

at the function parseLine, that is where you might split then line on "," into an array variable and then push that variable into a collection;

let tempArr = line.split(",");
globalArr.push(tempArr);