我正在尝试根据第一列中的vlue将csv文件分成多个组。然后将行存储在数组中以显示在网页上。 我是javascript新手,无法弄清楚。 我有一个具有以下结构的csv文件:
0,-1.0,0.0,0.0,0.0
1,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,-1.0,0.0,0.0,0.0
3,0.0,0.0,0.0,0.0
0,-1.0,0.0,0.0,0.0
1,-1.0,0.0,0.0,0.0
2,-1.0,0.0,0.0,0.0
3,-1.0,0.0,0.0,0.0
4,0.0,0.0,0.0,0.0
我正在尝试根据文件中第一列的值将数据行分成几组。我试图将0到下一个零进行分组,然后将拆分数据的每个块存储在数组中以显示在网页上。
我能够从for循环访问第一行并设置条件以检查该值是否为0,但是我无法添加下一行直到确定下一个0。
请参阅下面的内容,了解我到目前为止的情况。
// AJAX in the data file
$.ajax({
type: "GET",
url: "static/Dataframe.csv",
dataType: "text",
cache: false,
success: function(data) {
var arr1 = data.split(/\r\n|\n|\r/);
var array2 = [];
for (let i = 0; i < arr1.length; i++) {
arr1[i].split(',');
array2.push(arr1[i]);
if(arr1[i][0]== 0){
array2.pop()
}
}
// for debugging
// document.getElementById("dialog").innerHTML = arr1;
console.log(array2);
}
});
这是我到目前为止所得到的结果。
[0: "1,0.0,0.0,0.0,0.0"
1: "1,-1.0,0.0,0.0,0.0"
2: "2,0.0,0.0,0.0,0.0"
3: "1,-1.0,0.0,0.0,0.0"
4: "2,-1.0,0.0,0.0,0.0"
5: "3,0.0,0.0,0.0,0.0"
6: "1,-1.0,0.0,0.0,0.0"
7: "2,-1.0,0.0,0.0,0.0"
8: "3,-1.0,0.0,0.0,0.0"
9: "4,0.0,0.0,0.0,0.0"]
我需要的是:
[
[0, -1.0, 0.0, 0.0, 0.0, 1, 0.0, 0.0, 0.0, 0.0],
[0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, 0.0, 0.0, 0.0, 0.0],
[0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, -1.0, 0.0, 0.0, 0.0, 3, 0.0, 0.0, 0.0, 0.0],
[0, -1.0, 0.0, 0.0, 0.0, 1, -1.0, 0.0, 0.0, 0.0, 2, -1.0, 0.0, 0.0, 0.0, 3, -1.0, 0.0, 0.0, 0.0, 4, 0.0, 0.0, 0.0, 0.0]
]
我是javaScript的新手,但在弄清楚这一点时遇到了麻烦。 任何建议将不胜感激。
答案 0 :(得分:1)
您可以检查内部数组的第一个值,并将新数组添加到结果集中。
var data = '0,-1.0,0.0,0.0,0.0\n1,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,-1.0,0.0,0.0,0.0\n3,0.0,0.0,0.0,0.0\n0,-1.0,0.0,0.0,0.0\n1,-1.0,0.0,0.0,0.0\n2,-1.0,0.0,0.0,0.0\n3, -1.0, 0.0, 0.0, 0.0\n4, 0.0, 0.0, 0.0, 0.0',
result = data.split(/\r\n|\n|\r/).reduce((r, s) => {
var array = s.split(',').map(Number);
if (!array[0]) {
r.push([]);
}
r[r.length - 1].push(...array);
return r;
}, []);
console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
使用此
// AJAX in the data file
$.ajax({
type: "GET",
url: "static/Dataframe.csv",
dataType: "text",
cache: false,
success: function(data) {
var arr1 = data.split(/\r\n|\n|\r/);
var array2 = [];
for (let i = 0; i < arr1.length; i++) {
tmp = arr1[i].split(',');
array2.push(tmp);
if(arr1[i][0]== 0){
array2.pop()
}
}
// for debugging
// document.getElementById("dialog").innerHTML = arr1;
console.log(array2);
}
});
答案 2 :(得分:0)
// AJAX in the data file
// Just removed the for loop with the length and see the modified code.
$.ajax({
type: "GET",
url: "static/Dataframe.csv",
dataType: "text",
cache: false,
success: function(data) {
var arr1 = data.split(/\r\n|\n|\r/);
var array2 = [];
for (var items of arr1) {
tmp = items.split(',');
array2.push(tmp);
if(items[0]== 0){
array2.pop()
}
}
// for debugging
// document.getElementById("dialog").innerHTML = arr1;
console.log(array2);
}
});