有没有一种方法可以在Javascript中按行号将CSV数据存储在数组中?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有没有一种方法可以在Javascript中按行号将CSV数据存储在数组中?相关的知识,希望对你有一定的参考价值。
我已经获得了CSV格式的大量数据,需要以视觉和听觉方式呈现。我遇到的问题是我不知道如何将CSV文件中的各个行转换为不同的数组。例如,这是我拥有的CSV数据页面:
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,
我想为每一行都有一个数组,例如,一个数组以COM 1 A开头,另一行以COM 1 B等,等等...
我已经尝试过使用jQuery-3.4.1.min.js和jQuery.csv.js,但是运气不好! jQuery.csv.js的文档困扰了我,我尝试了...:
FruitNumCOM1A = $.csv.toArray(/*Line number in CSV file*/)
但是那显然不起作用!
我对Java语言还很陌生,所以任何帮助都会很棒!
答案
看看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);
在parseLine函数处,您可以在该处拆分,然后在“,”上将其行分成一个数组变量,然后将该变量推入一个集合中;
let tempArr = line.split(",");
globalArr.push(tempArr);
以上是关于有没有一种方法可以在Javascript中按行号将CSV数据存储在数组中?的主要内容,如果未能解决你的问题,请参考以下文章