Javascript 对值求和时返回 NaN 的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14422207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:41:48  来源:igfitidea点击:

Object returning NaN when sum values

javascript

提问by groc426

I'll admit I'm weak in JavaScript and JSON. I've spent a lot of time attempting to figure out why numbers from my objects returns NaN when they are added together. With that in mind, below is my JSON, stored to a variable:

我承认我在 JavaScript 和 JSON 方面很弱。我花了很多时间试图弄清楚为什么我的对象中的数字在它们相加时返回 NaN。考虑到这一点,下面是我的 JSON,存储到一个变量中:

var data = [
    {
        "acc_ext_id": null,
        "cat_code": 10002,
        "cat_ds": "REVENUE",
        "category_id": null,
        "chart_id": null,
        "created_at": null,
        "dept_id": null,
        "feb": null,
        "id": null,
        "jan": 30,
        "note": null,
        "total_cost": null,
        "updated_at": null,
        "year_id": null
    },
    {
        "acc_ext_id": "41260-02600",
        "cat_code": 10002,
        "cat_ds": "REVENUE",
        "category_id": 2,
        "chart_id": 2373,
        "created_at": "2013-01-15 16:43:52.169213",
        "dept_id": 86,
        "feb": 45,
        "id": 3,
        "jan": 60,
        "note": "Two",
        "total_cost": 105,
        "updated_at": "2013-01-15 16:43:52.169213",
        "year_id": 1
    }
]

I then attempt to iterate over the objects and sum the values:

然后我尝试遍历对象并对值求和:

var jan;

for (var i=0;i<data.length;i++){ 
    if(data[i].jan != null){    
        jan += parseFloat(data[i].jan);
        console.log(jan);
    }
}

Printed out in the console is NaN. I've attempted to parse the number as well as leave it raw, but to no avail. Is there something wrong with my objects? Here is a jsFiddle to illustrate: http://jsfiddle.net/5E2pm/3/

在控制台打印出来的是NaN. 我试图解析这个数字并保持原始状态,但无济于事。我的对象有问题吗?这是一个 jsFiddle 来说明:http: //jsfiddle.net/5E2pm/3/

回答by Sir

var jan = 0; //this should solve it

for (var i=0;i<data.length;i++){ 
    if(data[i].jan != null){    
        jan += parseFloat(data[i].jan);
        console.log(jan);
    }
}

Try this should solve it :)

试试这个应该可以解决它:)

Explanation as quoted by DON in comments below:

DON 在以下评论中引用的解释:

var jan; this will declare variable as undefined, so when you try to add values with undefined you will get as NaN, so the answer here with var jan = 0 will work – DON

var jan; 这会将变量声明为未定义,因此当您尝试添加未定义的值时,您将获得 NaN,因此此处使用 var jan = 0 的答案将起作用 – DON

回答by martinedwards

I like this approach. It basically sets the value to 0 on the first iteration when jan doesn't exist.

我喜欢这种方法。当 jan 不存在时,它基本上在第一次迭代时将值设置为 0。

jan = (jan || 0) + parseFloat(data[i].jan);

回答by sourcecode

you need to initialize janfirst

你需要初始化一月第一

var jan = 0; 

here's the example - link

这是示例 -链接