循环遍历json对象并返回汇总结果

时间:2016-07-29 06:42:47

标签: javascript json

我在计算json对象和返回平均结果时遇到问题。

这是我的json对象

var testJson = [{
                "1": "0.038728952407837",
                "2": "0.034420967102051",
                "3": "0.034113883972168",
                "4": "0.033237934112549",
                "5": "0.033545017242432",
                "6": "0.033923149108887",
                "7": "0.033990859985352",
                "8": "0.033454895019531",
                "9": "0.033518075942993",
                "10": "0.033759117126465",
                "11": "0.033965826034546",
                "12": "0.03358006477356",
                "13": "0.033926010131836",
                "14": "0.033300876617432",
                "15": "0.033140897750854",
                "16": "0.033447027206421",
                "17": "0.033830165863037",
                "18": "0.033417940139771",
                "19": "0.033578157424927",
                "20": "0.032893180847168",
            }]

这是我的代码

var arr = testJson[0];
var total = 0;
for (var i = 0; i < arr.length; i++) {  //loop through the array
    total += arr[i];  //Do the math!
}
console.log(total)

并且此结果的输出只是连接字符串

0.0387289524078370.0344209671020510.0341138839721680.0332379341125490.0335450172424320.0339231491088870.0339908599853520.0334548950195310.0335180759429930.0337591171264650.0339658260345460.033580064773560.0339260101318360.0333008766174320.0331408977508540.0334470272064210.0338301658630370.0334179401397710.0335781574249270.0328931808471680.0339531898498540.0339729785919190.0338070392608640.0332689285278320.0333919525146480.033372879028320.0353031158447270.0355949401855470.0359919071197510.036854982376099

那么,我在哪里失败?

4 个答案:

答案 0 :(得分:3)

这是一种类型转换问题。您在testJson上的值是字符串。 string1 + string2汇总这两个字符串并生成新字符串。您应该使用parseFloat函数将此值解析为Float。

if(!Object.values){Object.values=obj=>Object.keys(obj).map(key=>obj[key])}

var arr = Object.values(testJson[0]);
var total = 0;
for (var i = 0; i < arr.length; i++) {  //loop through the array
    total += parseFloat(arr[i]);  //Do the math!
}
console.log(total)

第二种选择;

if(!Object.values)Object.values=obj=>Object.keys(obj).map(key=>parseFloat(obj[key]))
var total = 0;
Object.values(testJson[0]).forEach(function(val){total += val});

我使用的代码行,

if(!Object.values)Object.values=obj=>Object.keys(obj).map(key=>parseFloat(obj[key]))

polyfill

  

来自维基百科;   在Web开发中,polyfill是实现功能的代码   不支持该功能的Web浏览器。大多数情况下,它指的是   一个实现HTML5 Web标准的JavaScript库,或者是   在旧浏览器上建立标准(由某些浏览器支持),   或现有的标准(任何浏览器都不支持)   浏览器。正式地,&#34; polyfill是浏览器API的垫片&#34;。[1]

答案 1 :(得分:2)

您需要将类型解析为float。因为json中的所有变量都是字符串。因此,当您使用添加功能时,它就像字符串连接一样工作。所以在添加之前解析为float。喜欢 总计+ = parseFloat(arr [i]);
for (var key in arr) { if (arr.hasOwnProperty(key)) { total += parseFloat(arr[key]); } } console.log(total);

答案 2 :(得分:1)

使用parseFloat。

var arr = testJson[0];
var total = 0;
for (var i = 0; i < arr.length; i++) {  //loop through the array
    **total += parseFloat(arr[i]);  //Do the math!**
}
console.log(total)

答案 3 :(得分:1)

var key, total = 0;
var testJson = [{
            "1": "0.038728952407837",
            "2": "0.034420967102051",
            "3": "0.034113883972168",
            "4": "0.033237934112549",
            "5": "0.033545017242432",
            "6": "0.033923149108887",
            "7": "0.033990859985352",
            "8": "0.033454895019531",
            "9": "0.033518075942993",
            "10": "0.033759117126465",
            "11": "0.033965826034546",
            "12": "0.03358006477356",
            "13": "0.033926010131836",
            "14": "0.033300876617432",
            "15": "0.033140897750854",
            "16": "0.033447027206421",
            "17": "0.033830165863037",
            "18": "0.033417940139771",
            "19": "0.033578157424927",
            "20": "0.032893180847168",
        }]
(testJson || []).forEach(function(obj){
    for(key in obj ){
       total += parseFloat(obj[key], 10);
    }
});