gpt4 book ai didi

javascript - csv[i] 未定义javascript

转载 作者:行者123 更新时间:2023-11-29 14:44:39 25 4
gpt4 key购买 nike

我正在使用 javascript 导入一个 csv 文件。它在我的程序中导入数据集,但我总是收到错误 csv[i] 未定义(第 57 行)。我尝试添加 vai i = 0;就在该行之前,但它仍然给我错误。知道我应该添加什么来消除这个错误吗?

//
// =============================================================
// CSV to coll - javascript code
// =============================================================
//
//
//
/***************************************************************
Notes:
This JS object is used to import the csv file and convert it for use within a coll.
Besides the original data, two spreads (between open and close and between high and low) are calculated
Furthermore, the minima and maxima of each column are sent out to allow contextualization of the values.
These minima and maxima can be used to set the range of the zmap object down the chain.

****************************************************************/
// =============================================================
// inlets and outlets
// =============================================================

outlets = 6;

// =============================================================
// Functions start here
// =============================================================

/***************************************************************
this function imports the csv file. The first line (the header row) is skipped and
the lines are converted to strings
****************************************************************/
function importfromfile(filename)
{
var f = new File(filename);
var csv = [];
var x = 0;
if (f.open) {
var str = f.readline(); //Skips first line.
while (f.position < f.eof) {
var str = f.readline();
csv.push(str);
}
f.close();
} else {
error("couldn't find the file ("+ filename +")\n");
}
/***************************************************************
1) the csv is read into the coll/cellblock
2) the spread between high-low and open-close is calculated and set out to the coll/cellblock as well
3) the maximum of each column is found and sent to outlet 1
****************************************************************/


var maxtimestamp=0;
var maxdatavalue=0;


for (var i=0; i<=csv.length; i++) {
var a = csv[i].split(",");
var timestamp = parseFloat(a[0]);
var datavalue = parseFloat(a[1]);


maxtimestamp=(timestamp>maxtimestamp)? timestamp : maxtimestamp; // open overwrites the max if it greater
maxdatavalue=(datavalue>maxdatavalue)? datavalue : maxdatavalue; // open overwrites the max if it greater

outlet(0, x++, timestamp, datavalue);
outlet(1, maxtimestamp, maxdatavalue);
outlet(4, csv.length);
}
// the minimum of each column is found and sent out to outlet 2
// a bang to outlet 3 makes sure that the coll is referred in the cellblock

var mintimestamp=Infinity;
var mindatavalue=0;

for (var i=0; i<=csv.length; i++) {
var a = csv[i].split(",");

var timestamp = parseFloat(a[0]);
var datavalue = parseFloat(a[1]);
mintimestamp=(timestamp<mintimestamp)? timestamp : mintimestamp; // open overwrites the min if it greater
datavalue=(datavalue<mindatavalue)? datavalue : mindatavalue; // open overwrites the min if it greater

outlet(2, mintimestamp, mindatavalue);
outlet(3, mintimestamp);
outlet(4, "bang");
}
}

最佳答案

问题是:

for (var i=0; i<=csv.length; i++) {
// -------------^

数组索引是从 0 到长度 - 1,而不是长度。删除 =。当 icsv.length 时访问 csv[i] 将得到 undefined,这将导致错误循环体的第一行,您尝试在其中调用 split

关于javascript - csv[i] 未定义javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34287021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com