【问题标题】:JavaScript - Creating an array for a knockout tournament-nextMatchIdJavaScript - 为淘汰赛创建一个数组-nextMatchId
【发布时间】:2022-01-22 14:06:11
【问题描述】:

我可以使用以下方法为淘汰赛生成数组。

以下是计算每轮比赛数的一般规则:

numberOfRounds = n    [ [2^n-1 matches], ...., [2^0 matches] ]

因此我知道,对于 8 支球队的锦标赛将有 3 轮,锦标赛将如下所示:

[ [4 matches], [2 matches], [1 match] ]

因此,如果我将 8 支球队的列表传递给锦标赛,将生成以下匹配数组:

// 方法

function genMatches (n) {
    let rounds = [];
    let indices = 1;
    let roundId = 0;
    while (n > 1) {
      roundId++;
      n = (n + 1) >> 1;
      var matches = [];

      for (var i = 0; i < n; ++i) {
        let match = {
          "roundid": roundId,
          "matchId": indices
        };
        matches.push(match);
        indices++;
      }

      let round = {};

      round['matches'] = matches;
      rounds.push(round);
    }
    return rounds;
}

// 输出

[
  {
    "matches": [
      {
        "roundid": 1,
        "matchId": 1
      },
      {
        "roundid": 1,
        "matchId": 2
      },
      {
        "roundid": 1,
        "matchId": 3
      },
      {
        "roundid": 1,
        "matchId": 4
      }
    ]
  },
  {
    "matches": [
      {
        "roundid": 2,
        "matchId": 5
      },
      {
        "roundid": 2,
        "matchId": 6
      }
    ]
  },
  {
    "matches": [
      {
        "roundid": 3,
        "matchId": 7
      }
    ]
  }
]

现在的问题是我想在每个匹配对象中都有 nextMatchId;

// 预期输出

[
  {
    "matches": [
      {
        "roundid": 1,
        "matchId": 1,
        "nextMatchId": 5
      },
      {
        "roundid": 1,
        "matchId": 2,
        "nextMatchId": 5
      },
      {
        "roundid": 1,
        "matchId": 3,
        "nextMatchId": 6
      },
      {
        "roundid": 1,
        "matchId": 4,
        "nextMatchId": 6
      }
    ]
  },
  {
    "matches": [
      {
        "roundid": 2,
        "matchId": 5,
        "nextMatchId": 7
      },
      {
        "roundid": 2,
        "matchId": 6,
        "nextMatchId": 7
      }
    ]
  },
  {
    "matches": [
      {
        "roundid": 3,
        "matchId": 7,
        "nextMatchId": null
      }
    ]
  }
]

任何帮助,非常感谢!

【问题讨论】:

    标签: javascript arrays angular


    【解决方案1】:

    因为:

    1. 您的数据始终是一棵树
    2. 每对匹配项都会进入下一轮的匹配项
    rounds.forEach((round, roundIndex) => {
        round.matches.forEach((match, matchIndex) => {
            match.nextMatchId = rounds[roundIndex + 1]?.matches[Math.floor(matchIndex / 2)]?.matchId ?? null;       
        });
    });
    

    这将产生您想要的输出:

    [{
            "matches": [{
                    "roundid": 1,
                    "matchId": 1,
                    "nextMatchId": 5
                }, {
                    "roundid": 1,
                    "matchId": 2,
                    "nextMatchId": 5
                }, {
                    "roundid": 1,
                    "matchId": 3,
                    "nextMatchId": 6
                }, {
                    "roundid": 1,
                    "matchId": 4,
                    "nextMatchId": 6
                }
            ]
        }, {
            "matches": [{
                    "roundid": 2,
                    "matchId": 5,
                    "nextMatchId": 7
                }, {
                    "roundid": 2,
                    "matchId": 6,
                    "nextMatchId": 7
                }
            ]
        }, {
            "matches": [{
                    "roundid": 3,
                    "matchId": 7,
                    "nextMatchId": null
                }
            ]
        }
    ]
    

    虽然我建议在生成时创建它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2013-12-18
      • 1970-01-01
      • 2014-01-25
      • 2013-02-12
      相关资源
      最近更新 更多