从具有相同对象字符串名称的JSON对象获取valurs

时间:2022-09-25 10:15:17

I'm getting my JSON object from the RIOT games API. Here is part of the JSON I get back:

我从RIOT游戏API获取了我的JSON对象。这是我回来的JSON的一部分:

{
"modifyDate": 1431043986000,
"champions": [
  {
     "id": 110,
     "stats": {
        "totalDeathsPerSession": 9,
        "totalSessionsPlayed": 1,
        "totalDamageTaken": 16507,
        "totalQuadraKills": 0,
        "totalTripleKills": 0,
        "totalMinionKills": 164,
        "maxChampionsKilled": 2,
        "totalDoubleKills": 0,
        "totalPhysicalDamageDealt": 101747,
        "totalChampionKills": 2,
        "totalAssists": 2,
        "mostChampionKillsPerSession": 2,
        "totalDamageDealt": 105995,
        "totalFirstBlood": 0,
        "totalSessionsLost": 1,
        "totalSessionsWon": 0,
        "totalMagicDamageDealt": 4248,
        "totalGoldEarned": 7932,
        "totalPentaKills": 0,
        "totalTurretsKilled": 0,
        "mostSpellsCast": 0,
        "maxNumDeaths": 9,
        "totalUnrealKills": 0
     }
  },
  {
     "id": 113,
     "stats": {
        "totalDeathsPerSession": 6,
        "totalSessionsPlayed": 1,
        "totalDamageTaken": 39226,
        "totalQuadraKills": 0,
        "totalTripleKills": 0,
        "totalMinionKills": 75,
        "maxChampionsKilled": 5,
        "totalDoubleKills": 1,
        "totalPhysicalDamageDealt": 28711,
        "totalChampionKills": 5,
        "totalAssists": 8,
        "mostChampionKillsPerSession": 5,
        "totalDamageDealt": 177132,
        "totalFirstBlood": 0,
        "totalSessionsLost": 1,
        "totalSessionsWon": 0,
        "totalMagicDamageDealt": 120299,
        "totalGoldEarned": 12544,
        "totalPentaKills": 0,
        "totalTurretsKilled": 1,
        "mostSpellsCast": 0,
        "maxNumDeaths": 6,
        "totalUnrealKills": 0
     }
  },

and so on for all of the champions in the game (there are around 150). If I wanted to calculate the total win/loss ratio or percent, I would have to find the total of "totalSessionsLost" and "totalSessionsWon". How would I go about totaling all of the values for this?

对于游戏中的所有冠军等等(大约有150个)。如果我想计算总赢/输率或百分比,我必须找到“totalSessionsLost”和“totalSessionsWon”的总和。我将如何总结所有的价值?

Thanks

1 个解决方案

#1


var total_win = 0,
    total_lost = 0,
    champions = yourjsonobj.champions;

for (var i = 0; i < champions.length; i++) {
    total_win += champions[i].totalSessionsWon;
    total_lost += champions[i].totalSessionsLost;
}

#1


var total_win = 0,
    total_lost = 0,
    champions = yourjsonobj.champions;

for (var i = 0; i < champions.length; i++) {
    total_win += champions[i].totalSessionsWon;
    total_lost += champions[i].totalSessionsLost;
}