我有一个正在使用的CSV文件。我不想将其转换为具有键和值的对象。我希望能够创建更多数组来存储不同的值 即温度 哼 小时 snowDepth等 所有这些都是具有7个值的数组
现在,这就是将csv存储在数据数组中的方式
- “ 2018年11月28日7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4”
- “ 2018年11月28日7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72”
- “ 2018年11月28日8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98”
- “ 2018年11月28日8:15,1131,-2.872,88,34.55,1.073,306.6,25.65”
- “ 2018年11月28日8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06”
- “ 2018年11月28日8:45,1133,-3.064,93.3,35,1.449,312.6,30.13”
- “ 2018年11月28日9:00,1134,-2.771,91.5,33.44,0.988,311,28.27” 长度:7
第一个列是时间戳记,第二个是记录号,第三个是温度,第四个是湿度,依此类推。
function get_Data(data) {
"use strict";
var fileData = new Array();
data = data.split(/\r\n|\r|\n/);
data = data.slice(Math.max(data.length - 7, 0));
createGraph(data);
}
我想要这样的数组
Tem = [-2.122,-2.325,-2.679,-2.872 ...]
小时= [7:30、7:45、8:00、8:15 ...]
答案 0 :(得分:2)
简单-每个逗号分别为map
和split
,然后查看它应属于哪个项目的索引:
var data = ["11/28/2018 7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4",
"11/28/2018 7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72",
"11/28/2018 8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98",
"11/28/2018 8:15,1131,-2.872,88,34.55,1.073,306.6,25.65",
"11/28/2018 8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06",
"11/28/2018 8:45,1133,-3.064,93.3,35,1.449,312.6,30.13",
"11/28/2018 9:00,1134,-2.771,91.5,33.44,0.988,311,28.27",
];
var items = {
date: [],
recordNumber: [],
temperature: [],
humidity: [],
value5: [],
value6: [],
value7: [],
value8: [],
};
Object.keys(items).forEach((key, index) => items[key].push(data.map(str => str.split(",")[index])));
console.log(items);
答案 1 :(得分:0)
尝试一下,它应该可以工作。我知道数据的位置,因此很容易拆分和查找条款和日期
var csv = document.getElementById("csvFile").innerHTML ;
function get_Data(data) {
var result = {terms:[], hours:[] }
var r = data.split(/\r\n|\r|\n/);
r.forEach(function(element) {
if (element !== "" && element.length>1){
var term = element.split(",")[2]
var date = new Date(element.split(",")[0])
var hour = date.getHours() + ":" + (date.getMinutes()<=9 ? "0" + date.getMinutes() : date.getMinutes());
result.terms.push(term);
result.hours.push(hour)
}
});
return result;
}
console.log( get_Data(csv))
<div id="csvFile" style="display:none;">
11/28/2018 7:30,1128,-2.122,86.2,34.06,1.178,320.2,20.4
11/28/2018 7:45,1129,-2.325,85.6,34.54,1.771,325.5,30.72
11/28/2018 8:00,1130,-2.679,85.7,30.73,1.764,312.1,28.98
11/28/2018 8:15,1131,-2.872,88,34.55,1.073,306.6,25.65
11/28/2018 8:30,1132,-2.953,90.7,35.25,1.247,311.2,30.06
11/28/2018 8:45,1133,-3.064,93.3,35,1.449,312.6,30.13
11/28/2018 9:00,1134,-2.771,91.5,33.44,0.988,311,28.27
</div>
答案 2 :(得分:0)
您正在混淆一些事情。 fileData
在做什么(似乎未使用)?而且slice
是执行此操作的不正确方法。
相反,请尝试:
function get_Data(csv) {
csv = csv.replace(/[\r\n]+/gi,"\n"); // reduce line endings into single LF chars
rows = csv.split("\n"); // split into array of rows
var result = [];
for (var i in rows) result.push(rows[i].split(",")); // split each row
return result;
}
(编辑)如果仅需要CSV数据的最后X行,请使用:
function get_Data(csv,lastNumRows) { // get only the last <lastNumRows> rows
csv = csv.replace(/[\r\n]+/gi,"\n"); // reduce line endings into single LF chars
rows = csv.split("\n").slice(-lastNumRows); // split into array of rows
var result = [];
for (var i in rows) result.push(rows[i].split(",")); // split each row
return result;
}