【问题标题】:Array returning NaN返回 NaN 的数组
【发布时间】:2015-08-03 21:50:28
【问题描述】:

我在为数组分配值时遇到问题,当我稍后访问该数组槽时,它返回 NaN。

首先,我声明数组如下:

    var oldTherms = []; 
    var newTherms = [];
    var oldInputTherms;
    var newInputTherms;

我正在使用变量数据集来计算要分配给 oldTherms 和 newTherms 数组的值。我已经验证了计算部分可以正常工作,并为 oldInputTherms 和 newInputTherms 提供了适当的值。我还验证了 dataSet.month[i] 返回正确的值。但是,下面的 switch 语句似乎没有将累积总数添加到 oldTherms[] 或 newTherms[]。当我尝试访问 newTherms[] 或 oldTherms[] 时,结果是 'NaN'

switch (dataSet.month[i]){
    //subtract 1 in array slot bc months number 1-12 and array slots number 0-11
    case 1: //january
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 2: //february
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 3: //march
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 4: //april
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 5: //may
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 6: //june
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 7:  //july
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 8: //august
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 9:  //sept
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 10: //oct
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 11: //nov
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    case 12:  //dec
        oldTherms[dataSet.month[i]-1] += oldInputTherms; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += newInputTherms;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
        break;
    default:
        oldTherms[dataSet.month[i]-1] += 0; //add therms to the total used 
        newTherms[dataSet.month[i]-1] += 0;
        oldKwh[dataSet.month[i]-1] += 0; //no cooling energy calcs
        newKwh[dataSet.month[i]-1] += 0;
}

【问题讨论】:

  • 为什么你需要所有做同样事情的案例?
  • oldInputTherms, newInputTherms 已声明,但未定义
  • default: 案例中的所有内容中添加0 有什么意义?
  • console.log(oldInputTherms) 放在switch 语句之前。

标签: javascript arrays nan


【解决方案1】:

当我尝试访问 newTherms[] 或 oldTherms[] 时,结果是 'NaN'

oldInputTherms, newInputTherms 已声明,但未定义,所以像这样的行

oldTherms[dataSet.month[i]-1] += oldInputTherms;
newTherms[dataSet.month[i]-1] += newInputTherms; 

会导致你的 NaN。


但是,你说

我已验证计算部分可以正常工作,并为 oldInputTherms 和 newInputTherms 提供了适当的值

如果是这样,那么可能oldThermsnewTherms 没有正确初始化。如果是这种情况,请参阅上面 Barmar 的回答。

【讨论】:

  • 抱歉有些混乱。我在代码的前面为 oldInputTherms 和 newInputTherms 赋值。我验证了在开关内它们仍然保持正确的值。我需要所有案例陈述,因为我将它们分成每个月的能源使用,所以我使用一个 12 槽数组来保存每个月的值。
  • 那只是给所有东西加0,没用。似乎代码中缺少一些应该将这些变量设置为相关的东西。
  • “无用”是一个相对术语。我们没有看到中间代码。如果 NaN 消失了,那么我们就在正确的轨道上。
  • 问题表明他已验证他在oldInputThermsnewInputTherms 中输入了适当的值
  • 让我们等待来自 OP 的更多代码,然后进行编辑。 NaN 还能如何从给定的代码中潜入这些数组?这是一个合理的观察。
【解决方案2】:

你需要初始化数组以包​​含0:

var oldTherms = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 
var newTherms = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 

否则,您将newInputThermsoldInputTherms 添加到undefined,结果是NaN

【讨论】:

  • 啊,太棒了。不错的地方。
  • 如果没有您指出正确的方向,就无法做到。
  • 完美!太感谢了!很抱歉给您带来任何困惑,感谢您的耐心等待。我只玩了几个星期的 JavaScript,还有很多东西要学!
猜你喜欢
  • 1970-01-01
  • 2022-11-03
  • 2020-07-14
  • 2019-07-28
  • 1970-01-01
  • 2014-04-21
  • 2015-11-26
  • 2012-09-08
相关资源
最近更新 更多