javascript - csv[i] 未定义javascript

标签 javascript csv

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

相关文章:

Python 3.2 在 csv.DictReader 中跳过一行

javascript - 如何从 API 访问数据?

javascript - 将 '+' 添加到 Algolia instantsearch rangeslider 最大值的末尾

javascript - GWT 中的 DOM(事件)

postgresql - 从标准输入复制 CSV 文件会抛出 "missing data for column"

c - 在 C 中对 CSV 文件进行排序

javascript - 如何在没有原生javascript join 的情况下手动实现 join 功能?

javascript - 一行中的多个严格相等比较

python - 我可以将具有多对一关系映射的 CSV 批量上传到 Django 吗?

sqlite - 导出不带 col.names 的 CSV