【发布时间】: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