csv[i] not defined javascript

csv[i] not defined javascript

本文关键字:defined javascript not csv      更新时间:2023-09-26

我正在使用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