【问题标题】:Last element of the array is always NaN even though I used Number() function即使我使用了 Number() 函数,数组的最后一个元素始终是 NaN
【发布时间】:2017-09-17 23:32:03
【问题描述】:

请帮助我调查下面的function,因为我被卡住了,仍然很难弄清楚。

一切都很好直到它到达nested FOR loop 的最后一列。 每一行的最后一列的值只有"0"。但是,我使用Number() 函数将单元格值(i.e. "0") 设置为number,但我继续获取 NaN 作为最后一个元素SUMCOUNT 数组。

colCount = 326rowCount = 374

sum.lengthcount.length 应该真的ONLY 325,因为headers不必要的 并且第一列只是由时间戳组成。我能够.push(0) 成功直到 nested FOR loop 更改最后一个元素的结果为NaN

function processDataToDictionary(csv) {
    var allTextLines = csv.split(/\r\n|\n/);

    var csvArray = [];

    for (let i = 0; i < allTextLines.length - 1; i++) {
        var row = allTextLines[i].split(',');

        csvArray.push(row);
    }

    var colCount = csvArray[0].length;
    var rowCount = csvArray.length;

    //Arrays of values
    var count = [];
    var sum = [];
    var average = [];
    var headers = [];

    for (let i = 1; i < colCount; i++) {
        var current = csvArray[0][i].replace(/"/g, '');
        sum.push(0);
        count.push(0);

        headers[i] = current;
    }

    for (let i = 1; i < rowCount; i++) {

        for (let j = 1; j < colCount; j++) {
            // Remove the quotes from your array
            current = csvArray[i][j].replace(/"/g, '');

            // Added the Method IsNullOrWhiteSpace
            if (!isNullOrWhitespace(current)) {
                // Parse as double not int to account for dec. values
                sum[j] += Number(current);
                count[j]++;
            }
        }
    }

    for (let i = 0; i < colCount; i++) {
        average.push((sum[i] + 0.0) / count[i]);
    }

    for (let i = 1; i < colCount; i++) {
     // create an empty array

        dictionary[headers[i]] = average[i];
    }

    return dictionary;

}

function isNullOrWhitespace(input) {

    if (input == " ") {

        return true;

    } else {

        return false;
    }
}

【问题讨论】:

  • 能否请您提供该函数的输入(参数),以便我可以在输入上运行它并重现问题?输入是否太大?你能把它上传到某个地方吗?
  • 从你所说的,我猜sum 数组的问题只能在这里:sum[j] += Number(current);。我猜headers 最后一个元素有问题,但如果不知道csvstring 是什么就很难知道。如果你能举一个csv的例子,那会很有帮助
  • 嗨@flen,这是csv 文件的链接:drive.google.com/open?id=0BzZ5wr0I2306SU5DdXF1b21IU2c 真的卡住了,请帮忙。谢谢!
  • @flen 同样的事情也发生在count数组:(奇怪
  • 顺便说一下,isNullOrWhitespace 在这里是未定义的,所以你所拥有的可能是:undefined + 0(或其他数字)将始终等于 NaN。您能否发布此方法或者这可能是代码中的错误?

标签: javascript arrays loops csv for-loop


【解决方案1】:

这为您提供了一个字典(对象),其中列名作为键,数字似乎是正确的平均值作为值。但是还是要检查某处的逻辑是否有问题,实际上平均值是不正确的。

function processDataToDictionary(csv) {

function isNullOrWhitespace(input) {

    if (input === " ") {

        return true;

    } else if (input === null) {

        return true;

    //} else if (input === undefined) {

        //return true;

    }   else {

        return false;
    }
}


var allTextLines = csv.split(/\r\n|\n/);

    var csvArray = [];

    for (let i = 0; i < allTextLines.length - 1; i++) {
        var row = allTextLines[i].split(',');

        csvArray.push(row);
    }

    var colCount = csvArray[0].length;
    var rowCount = csvArray.length;

    //Arrays of values
    var count = [];
    var sum = [];
    var average = [];
    var headers = [];

    for (let i = 1; i < colCount; i++) {
        var current = csvArray[0][i].replace(/"/g, '');
        sum.push(0);
        count.push(0);

        headers[i] = current;
    }

    /**** I added these two lines ****/
    sum.push(0);
    count.push(0);

for (let i = 1; i < rowCount; i++) {

        for (let j = 1; j < colCount ; j++) {

            // Remove the quotes from your array
            current = csvArray[i][j].replace(/"/g, '');

            // Added the Method IsNullOrWhiteSpace
            if (!isNullOrWhitespace(current)) {
                // Parse as double not int to account for dec. values
                sum[j] += Number(current);
                count[j]++;
            }
        }
    }


     for (let i = 0; i < colCount; i++) {
        average.push((sum[i] + 0.0) / count[i]);
    }


// I added this line:           
dictionary = {};    

 for (let i = 1; i < colCount; i++) {

        dictionary[headers[i]] = average[i];
    }


    return dictionary;

}

让我知道这是否适合您。您可以使用以下命令遍历这些值:for (let key in dictionary) {console.log("key: " + key + " , value: " + dictionary[key]);}。问候!

【讨论】:

  • 但它只是删除了最后一个元素System\Processor Queue Length
  • 我查看了与最后一列具有相同精确值的其他列并且它们有效,结果给出了0。数组的最后一个元素一定有什么东西 mhmmm
  • 问题在于第二个 for 循环会使 sumcount 的长度缩短一个元素。然后当求和循环运行时,它不会在最后一个位置找到任何值(因为 length -=1 ),并且会得到undefined。然后 sum + undefined 将返回 NaN
  • 完全正确!!!我也想通了现在!哈哈,兄弟将您的编辑还原,以便我将其标记为答案! :)
  • 没问题!:) 虽然我认为这个版本现在更整洁(这样,header[0] != undefined),但两者都可以正常工作
猜你喜欢
  • 2020-09-26
  • 2020-06-13
  • 2022-11-21
  • 2013-02-03
  • 2021-08-18
  • 1970-01-01
  • 2021-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多