如何在做数学运算时聚合两个json数据

时间:2019-06-17 04:27:29

标签: javascript json

我有两个json文件,其中包含所有球队名称和代码,第二个包含比赛详细信息和得分。我想制作另一个json,该json汇总总比赛胜负总进球数 json1:https://api.jsonbin.io/b/5d07165d4f234842a566d0b4 json2:https://api.jsonbin.io/b/5d07168c2808a77fb80a2254

我试图使用... rest运算符。

杰森1

> The list comprehension always returns a result list. 
> 
> But in your nested loop case you are not returning list so little
> modify in your case
> 
> 
  def combinations(array,n):
>     if n == 0:
>         return [[]]
>     else:
          result=[]
>         for i in array:
>             for j in combinations(array, n-1):
>                 result.append([j + [i]])
>         return result 
 In this you are returning the list of all iterations ..

Json2

{
"name": "English Premier League 2011/12",
"clubs": [
{
"key": "chelsea",
"name": "Chelsea",
"code": "CHE"
},
{
"key": "arsenal",
"name": "Arsenal",
"code": "ARS"
},
{
"key": "tottenham",
"name": "Tottenham Hotspur",
"code": "TOT"
}
]
}

预期的JSON结果

{
  "name": "English Premier League 2011/12",
  "rounds": [
    {
      "name": "Round 1",
      "matches": [
        {
          "date": "2012-01-11",
          "team1": {
            "key": "tottenham",
            "name": "Tottenham Hotspur",
            "code": "TOT"
          },
          "team2": {
            "key": "everton",
            "name": "Everton",
            "code": "EVE"
          },
          "score1": 2,
          "score2": 0
        },
        {
          "date": "2011-08-15",
          "team1": {
            "key": "mancity",
            "name": "Manchester City",
            "code": "MCI"
          },
          "team2": {
            "key": "swansea",
            "name": "Swansea",
            "code": "SWA"
          },
          "score1": 4,
          "score2": 0
        },
        {
          "date": "2011-08-14",
          "team1": {
            "key": "westbrom",
            "name": "West Bromwich Albion",
            "code": "WBA"
          },
          "team2": {
            "key": "manutd",
            "name": "Manchester United",
            "code": "MUN"
          },
          "score1": 1,
          "score2": 2
        },
        {
          "date": "2011-08-14",
          "team1": {
            "key": "stoke",
            "name": "Stoke City",
            "code": "STK"
          },
          "team2": {
            "key": "chelsea",
            "name": "Chelsea",
            "code": "CHE"
          },
          "score1": 0,
          "score2": 0
        }
]
}
]
}

1 个答案:

答案 0 :(得分:0)

类似的事情可能会帮助您添加额外的错误检查和少量修改以获得准确的结果。

let a = {
    "name": "English Premier League 2011/12",
    "clubs": [{
            "key": "chelsea",
            "name": "Chelsea",
            "code": "CHE"
        },
        {
            "key": "arsenal",
            "name": "Arsenal",
            "code": "ARS"
        },
        {
            "key": "tottenham",
            "name": "Tottenham Hotspur",
            "code": "TOT"
        }
    ]
}
let b = {
    "name": "English Premier League 2011/12",
    "rounds": [{
        "name": "Round 1",
        "matches": [{
                "date": "2012-01-11",
                "team1": {
                    "key": "tottenham",
                    "name": "Tottenham Hotspur",
                    "code": "TOT"
                },
                "team2": {
                    "key": "everton",
                    "name": "Everton",
                    "code": "EVE"
                },
                "score1": 2,
                "score2": 0
            },
            {
                "date": "2011-08-15",
                "team1": {
                    "key": "mancity",
                    "name": "Manchester City",
                    "code": "MCI"
                },
                "team2": {
                    "key": "swansea",
                    "name": "Swansea",
                    "code": "SWA"
                },
                "score1": 4,
                "score2": 0
            },
            {
                "date": "2011-08-14",
                "team1": {
                    "key": "westbrom",
                    "name": "West Bromwich Albion",
                    "code": "WBA"
                },
                "team2": {
                    "key": "manutd",
                    "name": "Manchester United",
                    "code": "MUN"
                },
                "score1": 1,
                "score2": 2
            },
            {
                "date": "2011-08-14",
                "team1": {
                    "key": "stoke",
                    "name": "Stoke City",
                    "code": "STK"
                },
                "team2": {
                    "key": "chelsea",
                    "name": "Chelsea",
                    "code": "CHE"
                },
                "score1": 0,
                "score2": 0
            }
        ]
    }]
}
let teamCodes = a.clubs.map(e => e['code']);
console.log("Team codes");
console.log(teamCodes)
let output = {'clubs': []};
teamCodes.map(e=> output.clubs.push({"code": e}))
b.rounds[0].matches.map(ef => {
    var total_matches = 0
    var won  = 0
    var lost = 0
    var tie  = 0
    var total_goals = 0
    teamCodes.forEach(function(e, index){
        var codeIndex = output.clubs.findIndex(function(element, index, array){return element.code ===e})
            
        if(ef.team1.code == e){            
            output.clubs[codeIndex].code = e
            output.clubs[codeIndex].name = ef.team1.name;
            output.clubs[codeIndex].key = ef.team1.key;
            output.clubs[codeIndex].total_matches = total_matches + 1
            output.clubs[codeIndex].total_goals = total_goals + ef.score1
            if(ef.score1 > ef.score2) {
                output.clubs[codeIndex].won = won + 1
            } else if(ef.score1 < ef.score2) {
                output.clubs[codeIndex].lost = lost + 1
            } else {
                output.clubs[codeIndex].tie = tie + 1
            }


        } else if(ef.team2.code == e) {
            output.clubs[codeIndex].code = e
            output.clubs[codeIndex].name = ef.team2.name;
            output.clubs[codeIndex].key = ef.team2.key;
            output.clubs[codeIndex].total_matches = total_matches + 1
            output.clubs[codeIndex].total_goals = total_goals + ef.score2
            if(ef.score1 > ef.score2) {
                output.clubs[codeIndex].won = won + 1
            } else if(ef.score1 < ef.score2) {
                output.clubs[codeIndex].lost = lost + 1
            } else {
                output.clubs[codeIndex].tie = tie + 1
            }
        }
    })
})
console.log("Output")
console.dir(output)