【问题标题】:Convert played matches to league table将已进行的比赛转换为联赛表
【发布时间】:2020-07-07 15:35:44
【问题描述】:

我参加在线 FIFA 锦标赛,因为里面的每个人都很安全。我想将结果转换为排行榜。但我不知道最好的方法是什么。

我拥有的数据是:

matches = [
        [{
            "homeTeam": "A",
            "awayTeam": "B",
            "homeGoals": 0,
            "awayGoals": 3
        },
        {
            "homeTeam": "D",
            "awayTeam": "C",
            "homeGoals": 0,
            "awayGoals": 3
        }],
        [{
            "homeTeam": "D",
            "awayTeam": "B",
            "homeGoals": 0,
            "awayGoals": 2
        },
        {
            "homeTeam": "A",
            "awayTeam": "C",
            "homeGoals": 0,
            "awayGoals": 1
        }]
    ];

我要显示的统计数据是:比赛次数、获胜次数、失败次数和平局次数。进球数和失球数。 我有一个基于 React 的网络应用程序,我在其中显示比赛的结果。 我通过 Firebase 托管 Web 应用程序,这样我就可以使用 Firestore,但这是最好的方式吗?

【问题讨论】:

    标签: node.js reactjs firebase html-table statistics


    【解决方案1】:

    当然可以使用 Firebase。为此,我将创建一个类来生成所需的数据结构并在其中包含操作数据所需的函数。

    League.js

    class League {
      constructor(matches) {
        this.matches = matches;
        this.table = {};
      }
    
      getStandings() {
        this.matches.forEach(match => {
          const { homeTeam, awayTeam } = match;
    
          // add teams to the table
          if (!this.table[homeTeam]) this.addToTable(homeTeam);
          if (!this.table[awayTeam]) this.addToTable(awayTeam);
    
          // increase the played counter
          this.increasePlayed([homeTeam, awayTeam]);
          // calculate won,lost, drawn
          this.setResults(match);
          // calculate goalsScored and goalsAgainst
          this.setGoals(homeTeam, match.homeGoals, match.awayGoals);
          this.setGoals(awayTeam, match.awayGoals, match.homeGoals);
        });
        // all is done; return the table
        return this.table;
      }
    
      addToTable(team) {
        this.table[team] = {
          played: 0,
          won: 0,
          lost: 0,
          drawn: 0,
          goalsScored: 0,
          goalsAgainst: 0
        };
      }
    
      increasePlayed(teams) {
        teams.forEach(team => this.table[team].played++);
      }
    
      setResults(match) {
        const {
          homeTeam, awayTeam, homeGoals, awayGoals
        } = match;
    
        if (homeGoals > awayGoals) {
          this.table[homeTeam].won++;
          this.table[awayTeam].lost++;
        } else if (homeGoals < awayGoals) {
          this.table[awayTeam].won++;
          this.table[homeTeam].lost++;
        } else {
          this.table[homeTeam].drawn++;
          this.table[awayTeam].drawn++;
        }
      }
    
      setGoals(team, scored, against) {
        this.table[team].goalsScored += scored;
        this.table[team].goalsAgainst += against;
      }
    }
    
    module.exports = League;
    

    然后在你需要 League 的任何地方,使用 matches 参数创建它的实例,然后只需调用 getStandings() 函数来计算并返回表格。

    app.js

    const League = require('./League');
    // note that all of the matches objects are flat
    const matches = [
      {
        homeTeam: 'A',
        awayTeam: 'B',
        homeGoals: 0,
        awayGoals: 3
      },
      {
        homeTeam: 'D',
        awayTeam: 'C',
        homeGoals: 0,
        awayGoals: 3
      },
      {
        homeTeam: 'D',
        awayTeam: 'B',
        homeGoals: 0,
        awayGoals: 2
      },
      {
        homeTeam: 'A',
        awayTeam: 'C',
        homeGoals: 0,
        awayGoals: 1
      }
    ];
    
    const league = new League(matches);
    const standings = league.getStandings();
    
    console.log(standings);
    

    现在运行 app.js 会输出:

    {
      A: {
        played: 2,
        won: 0,
        lost: 2,
        drawn: 0,
        goalsScored: 0,
        goalsAgainst: 4
      },
      B: {
        played: 2,
        won: 2,
        lost: 0,
        drawn: 0,
        goalsScored: 5,
        goalsAgainst: 0
      },
      D: {
        played: 2,
        won: 0,
        lost: 2,
        drawn: 0,
        goalsScored: 0,
        goalsAgainst: 5
      },
      C: {
        played: 2,
        won: 2,
        lost: 0,
        drawn: 0,
        goalsScored: 4,
        goalsAgainst: 0
      }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-30
      • 2015-10-07
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多