数组返回 NaN

Array returning NaN

我在为数组赋值时遇到问题,当我稍后访问该数组槽时,它 returns 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;
}

When I try to access the newTherms[] or oldTherms[], the result is 'NaN'

oldInputThermsnewInputTherms 已声明,但未定义,因此像这样的行

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

会导致你的 NaN。


但是,你说

I've verified that the calculation portion works and supplies an appropriate value into oldInputTherms and newInputTherms

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

您需要初始化数组以包​​含 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