0

我很难通过索引将对象推送到数组数组。在我当前的(非常重复的代码)下面找到它读取 CSV 文件的行(格式:日期,reasonCode),然后根据 reasonCode 创建 FROM 和 TO(日期)对。然后将该数组用于 Highcharts(甘特图)。请注意fromto1andfromto2数组。

csv = csv.split(/\n/g);
var fromto1 = [];                   //array of FROM and TO pairs of code 1
    fromto2 = [];                   //array of FROM and TO pairs of code 2
    count = [];
    lastFrom = [];

for (var i=1;i<3;i++) {                 //set all count and lastFrom variables to 0     //bs
    count[i] = 0;
    lastFrom[i] = 0;
}

jQuery.each(csv, function(i, line) {

    line = line.split(',');                 //splits line, returns array of splitted values
    date = parseInt(line[0], 10)*1000;      //read date from line into string
    reasonC = parseInt(line[2], 10);        //read reasonC from line into string

if (reasonC == "1") {
    count[1]++;
    if (count[1] % 2 !=0){          //if it is an uneven value (FROM values)  
        lastFrom[1] = date;         //temporary save the date in lastFrom[]
    }
    else {                          //if it is an even value (TO value), push the pair
        fromto2.push({ 
            from: lastFrom[1],
            to: date
        });
    }   
}

if (reasonC == "2") {
    count[2]++;
    if (count[2] % 2 !=0){          
        lastFrom[2] = date;         
    }
    else {                          
        fromto3.push({ 
            from: lastFrom[2],
            to: date
        });
    }
}

为什么我不能用这个替换上面的代码(请注意fromto数组的数组):

csv = csv.split(/\n/g);
var fromto = [];                    
    count = [];
    lastFrom = [];

for (var i=1;i<3;i++) {                 //set all count and lastFrom variables to 0     
    count[i] = 0;
    lastFrom[i] = 0;
    fromto.push(new Array());
    console.log(i+': New Array Pushed');
}

jQuery.each(csv, function(i, line) {

    line = line.split(',');                 //splits line, returns array of splitted values
    date = parseInt(line[0], 10)*1000;      //read date from line into string
    reasonC = parseInt(line[2], 10);        //read reasonC from line into string

    for (var c=1;c<3;c++) {
        if (reasonC == c.toString()) {
            count[c]++;
            if (count[c] % 2 !=0){          //if it is an uneven value (FROM values)  
                lastFrom[c] = date;         //temporary save the date in lastFrom[]
            }
            else {                          //if it is an even value (TO value), push the pair
                fromto[c].push({ 
                    from: lastFrom[c],
                    to: date
                });
            }   
        }
    }
}

我相信问题在于fromto[c].push({它保持空白数组。我仍然是一个 Jsnoob,在其他线程上找不到任何答案,非常感谢您的帮助

4

1 回答 1

0

您的 JavaScript 中有很多事情可以通过一些提示来完成,这些提示是关于如何使用最佳实践来做您想做的事情,所以一个答案 + 建议。

1) 单个声明中的多个变量用逗号分隔,而不是分号:

var csv = csv.split(/\n/g),
    fromto = [],
    count = [],
    lastFrom = [];

2) 不要使用 Array 对象来制作数组。

fromto.push([]);

3) JS只有在你使用var. 没有var,变量是全局的。

jQuery.each(csv, function(i, line) {
  line = line.split(',');
  var date = parseInt(line[0], 10)*1000,
      reasonC = parseInt(line[2], 10);

4) == 是一个强制相等,并且将查看是否有任何方法可以将两个值视为相同。4 == "4" 是真的,4 === "4" 不是。

if (reasonC == c) { ...

或者

if (reasonC === c.toString()) { ...

5) JavaScript 已经融入了 forEach,你为什么还要使用 jQuery 来处理已经是 JavaScript 一部分的东西?

csv.forEach(function(line) {
  ...
});

然后是你问题的答案,当然。看起来您正在尝试转换此数据:

123,somevalue,1
456,somevalue,2
...

进入这个结构:

[
  { from: 123, to: 456 },
  { from: ..., to: ...},
  ...
] 

您依靠线路顺序来告诉您哪个日期是从哪个是到;这肯定会出错,但您最了解自己的数据。(如果我写这个,我会假设行顺序是未知的)

var cvs = data.split(",");
    parity = 0,
    fields,
    odd = [],
    even = [],
    fromTo = [];

cvs.forEach(function(line) {
  parity = (parity + 1) % 2;
  fields = line.split(",");
  bin = parseInt(fields[2],10);
  if(parity===1) {
    odd[bin] = fields;
  } else {
    even[bin] = fields;
    fromTo[bin].push({
      from: odd[bin][0],
      to: even[bin][0]
    });
  }
});

现在,这应该可以工作,但这段代码也很可怕,因为它仍然依赖于 CVS 文件中的行顺序,并且没有硬性保证是这种情况(如预处理验证),这(如你的代码)会做的很糟糕错误的事情发生在两行意外地顺序错误的那一刻。

于 2013-09-02T17:40:45.613 回答