对多个数组中的多个对象求和,然后求平均值

时间:2016-10-24 12:12:36

标签: javascript arrays

我有一个名字,比赛号码(1,2,3)的对象和多个司机的终点位置。我想迭代每个数组并显示(console.log很好)比赛号码,结束位置,然后是每个车手的平均结束位置。

以下是数据和所需的输出:

{
"drivers":[
    {
        "Name":"Smith",
            "races": [
            {
                "race 1":"12"
            },
            {
                "race 2":"6"
            },
            {
                "race 3":"10"
            }
        ]
    },
    {
        "Name":"Jones",
            "races": [
            {
                "race 1":"11"
            },
            {
                "race 2":"6"
            },
            {
                "race 3":"5"
            }
            ]
        }
    ]
}

输出:

    Smith:
    Race 1 finishing position: 12
    Race 2 finishing position: 6
    Race 3 finishing position: 10
    Average finishing position: 9.3

    Jones:
    Race 1 finishing position: 11
    Race 2 finishing position: 6
    Race 3 finishing position: 5
    Average finishing position: 7.3

3 个答案:

答案 0 :(得分:3)

使用Array#forEach来迭代array 使用Object.keys()获取Object

的键



var obj = {
  "drivers": [{
    "Name": "Smith",
    "races": [{
      "race 1": "12"
    }, {
      "race 2": "6"
    }, {
      "race 3": "10"
    }]
  }, {
    "Name": "Jones",
    "races": [{
      "race 1": "11"
    }, {
      "race 2": "6"
    }, {
      "race 3": "5"
    }]
  }]
};
obj.drivers.forEach(function(el) {
  console.log(el.Name + ':');
  var total = 0;
  el.races.forEach(function(race) {
    var key = Object.keys(race)[0];
    total += +race[key];
    console.log(key + ' finishing position :' + race[key]);
  });
  console.log('Average finishing position :' + total / el.races.length);
});




答案 1 :(得分:0)

我只是循环它并且让比赛没有使用Object.keys()

 var data = {
        "drivers":[
        {
            "Name":"Smith",
                "races": [
                {
                    "race 1":"12"
                },
                {
                    "race 2":"6"
                },
                {
                    "race 3":"10"
                }
            ]
        },
        {
            "Name":"Jones",
                "races": [
                {
                    "race 1":"11"
                },
                {
                    "race 2":"6"
                },
                {
                    "race 3":"5"
                }
                ]
            }
        ]
    };


    for(var x in data.drivers) {
        console.log(data.drivers[x].Name + ":");
        var total = 0;
        for(var i in data.drivers[x].races) {
            var key = Object.keys(data.drivers[x].races[i]);
            total += parseInt(data.drivers[x].races[i][key]);
            console.log(key+ " finishing: "+data.drivers[x].races[i][key]);
        }
        console.log('Average finishing position: ' + (total/3).toFixed(1));
    }

答案 2 :(得分:0)

这个对于任何数量的用户及其在JSON对象中的比赛是通用的

jsonObj.drivers.forEach(function(obj) { var len = obj.races.length; console.log(obj.Name+":"); var avrg = 0 for(var j=0;j<obj.races.length;j++){ var x = j+1; var key = "race "+x; var race = parseInt(obj.races[j][key]); avrg +=race; console.log("Race "+(j+1)+" finishing position: "+race); } console.log("Average finishing position: "+(avrg/len).toPrecision(2)); });