【问题标题】:Amount of all goals of one team (from JSON)一个团队的所有目标的数量(来自 JSON)
【发布时间】:2020-03-17 04:07:18
【问题描述】:

我有一个包含足球比赛数据的 JSON 文件

[
   {
      "game_id":"258716",
      "game_date_start":"2016-08-15",
      "season": "2016",
      "team_1_id":"119",
      "team_2_id":"120",      
      "team_1_goals_quantity":"2",
      "team_2_goals_quantity":"1",         
   },
   {
      "game_id":"258717",
      "game_date_start":"2016-09-15",
      "season": "2016",
      "team_1_id":"119",
      "team_2_id":"122",      
      "team_1_goals_quantity":"1",
      "team_2_goals_quantity":"1"     
   },
   {
      "game_id":"258718",
      "game_date_start":"2016-07-15",
      "season": "2016",
      "team_1_id":"121",
      "team_2_id":"119",     
      "team_1_goals_quantity":"1",
      "team_2_goals_quantity":"1"      
   }
]

任务 #1。统计每支球队的总比赛场数

任务 #2。计算球队的总进球数(在所有比赛中)

我正在使用 Vue.js。我已经通过这种方式解决了任务 #1:

 countGames() {     
            var data = this.$store.getters.games; 
            var arr1 = []; 
            var obj1 = {}; 

            //Here I got all unique Team IDs
            function getMatches() { 
                    for (var i in data) {        
                          obj1[data[i].team_1_id] = (obj1[data[i].team_1_id] || 0) + 1;
                          obj1[data[i].team_2_id] = (obj1[data[i].team_2_id] || 0) + 1
                    };      
                    Object.keys(obj1).forEach(function(el, data) {
                        arr1.push( [ el, obj1[el]] );                       
                    });                          
                 }; 
            getMatches();          
            var result = arr1.map(
                  ([team_id, matches]) => ({team_id, matches}) //here I got { {team_id: "119", matches: 3}, {team_id: "120", matches: 1} ... }
            );
            return result;                 
        }  

它有效。但我不知道如何解决任务 #2 - 计算每支球队在所有比赛中的总进球数。问题是,有时团队的唯一 ID 可能位于 team_1_id,有时位于 team_2_id(如 JSON 示例中的团队“119”i)。

请帮我写这个脚本。

【问题讨论】:

  • game_id 总是一样的?应该不一样吧?
  • 是的,每次都不一样,谢谢前注。

标签: javascript json vue.js


【解决方案1】:

如果您坚持保持代码不变,您可能会添加另一个对象来跟踪目标,如下所示:

 function countGames() {
      var data = this.$store.getters.games;      
      var arr1 = []; 
      var goalsPerTeam = []; // keep track of goals here
      var obj1 = {}; 

      //Here I got all unique Team IDs
      function getMatches() { 
              for (var i in data) {        
                    obj1[data[i].team_1_id] = (obj1[data[i].team_1_id] || 0) + 1;
                    obj1[data[i].team_2_id] = (obj1[data[i].team_2_id] || 0) + 1;
                    // add goals count
                    if (goalsPerTeam[data[i].team_1_id] === undefined) {
                      goalsPerTeam[data[i].team_1_id] = parseInt(data[i].team_1_goals_quantity);
                    } else {
                      goalsPerTeam[data[i].team_1_id] += parseInt(data[i].team_1_goals_quantity);
                    }

                    if (goalsPerTeam[data[i].team_2_id] === undefined) {
                      goalsPerTeam[data[i].team_2_id] = parseInt(data[i].team_2_goals_quantity);
                    } else {
                      goalsPerTeam[data[i].team_2_id] += parseInt(data[i].team_2_goals_quantity);
                    }
              };      
              Object.keys(obj1).forEach(function(el, data) {
                  arr1.push( [ el, obj1[el], goalsPerTeam[el]] );  // update this                   
              });                          
           }; 
        getMatches();          
        var result = arr1.map(
              // add here finally
              ([team_id, matches, goals]) => ({team_id, matches, goals}) //here I got { {team_id: "119", matches: 3}, {team_id: "120", matches: 1} ... }
        );
        return result;                 
      }  

【讨论】:

  • 您的解决方案对我有用,但只是部分。不知何故,它只计算球队上一场比赛的进球数,而不是所有进球的总和。所以我得到了“119”:1个进球,而不是“119”:3个进球。你能说出可能是什么原因吗?
  • 对不起,之前没测试好。请检查编辑后的答案。使用此示例数据,它得出,即团队“119”:4 个进球 (2 + 1 + 1)。
【解决方案2】:

你可以这样做。

const getResultsByTeamId = (id) => {
  const matches = list
    .filter((match) => match.team_1_id === id || match.team_2_id === id)

  const goals = matches
    .map((match) => match.team_1_id === id
      ? match.team_1_goals_quantity
      : match.team_2_goals_quantity)
    .reduce((acc, goals) => acc + parseInt(goals), 0)

  return {
    matches: matches.length,
    goals
  }
}

然后调用

// The argument is a string because your ID is a string, not a number.
getResultsByTeamId('119') #returns { matches: 3, goals: 4 }
getResultsByTeamId('120') #returns { matches: 1, goals: 1 }
getResultsByTeamId('121') #returns { matches: 1, goals: 1 }
getResultsByTeamId('122') #returns { matches: 1, goals: 1 }

快速解释:首先您过滤将球队作为球队 1 或球队 2 的比赛。然后您只获得球队的目标,然后求和。

【讨论】:

    【解决方案3】:

    只关注目标的计数,我的解决方案是创建一个仅包含团队目标对的字典,然后将其汇总到一个团队目标编号数组中:

    function count_team_goals(json){
    
       // create a summary json
       let summ = []
       for(i in json){
          team_1 = {'team': json[i]['team_1_id'], 'goals': json[i]['team_1_goals_quantity']};
          team_2 = {'team': json[i]['team_2_id'], 'goals': json[i]['team_2_goals_quantity']};
          summ.push(team_1)
          summ.push(team_2);
       }
    
       // count the team goals with reduce and return
       let summ_d = summ.reduce(function(e, c) {
          e[c.team] = e[c.team] || {'team_id': c.team, 'goals': 0};
          e[c.team]['goals'] = e[c.team]['goals'] + parseInt(c.goals);
          return e;
       }, {})
    
       // return array
       return Object.keys(summ_d).map(k => summ_d[k])
    }
    

    【讨论】:

      【解决方案4】:

      您可以映射数组的每个对象并增加相应的计数器以获取您的数据,如下所示:

          let totalGames = {},
              totalGoals = {};
      
          data.map( game => {
      
              //count games team 1
              if (!totalGames.hasOwnProperty(game.team_1_id)) {
                  totalGames[game.team_1_id] = 1;
              } else {
                  totalGames[game.team_1_id] += 1;
              }
              //count games team 2
              if (!totalGames.hasOwnProperty(game.team_2_id)) {
                  totalGames[game.team_2_id] = 1;
              } else {
                  totalGames[game.team_2_id] += 1;
              }
      
              //count goals team 1
              if (!totalGoals.hasOwnProperty(game.team_1_id)) {
                  totalGoals[game.team_1_id] = parseInt(game.team_1_goals_quantity);
              } else {
                  totalGoals[game.team_1_id] += parseInt(game.team_1_goals_quantity);
              }
      
              //count goals team 2
              if (!totalGoals.hasOwnProperty(game.team_2_id)) {
                  totalGoals[game.team_2_id] = parseInt(game.team_2_goals_quantity);
              } else {
                  totalGoals[game.team_2_id] += parseInt(game.team_2_goals_quantity);
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-11-17
        • 2020-11-20
        • 1970-01-01
        • 2021-01-06
        • 2015-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多