我正在努力让我最近的Valorant Premier比赛变得一应俱全.

问题是,我不能以任何方式对数组进行排序,它似乎只是自动排序,我不知道在哪里.

我的计划是对值为pointsBeforepointsAfter的数组进行排序,这样最新的匹配项(具有最高值)位于顶部,而第一个匹配项位于底部(最低值).

我希望你能帮助我!

async function fetchMatchData(matchId) {
  const matchUrl = `https://api.henrikdev.xyz/valorant/v2/match/${matchId}`;
  const response = await fetch(matchUrl);
  const matchData = await response.json();
  return matchData.data;
}

async function fetchAndDisplayMatches() {
  const historyUrl = "https://api.henrikdev.xyz/valorant/v1/premier/70921669-f22e-4a59-9e26-f19664a1fa20/history";
  const response = await fetch(historyUrl);
  const historyData = await response.json();
  const leagueMatches = historyData.data.league_matches;

  // Sort the matches array based on points_before in descending order
  leagueMatches.sort((a, b) => b.points_before - a.points_before);

  const matchHistoryElement = document.getElementById("match-history");

  leagueMatches.forEach(async(match) => {
    const matchId = match.id;
    const matchData = await fetchMatchData(matchId);

    const redTeamName = matchData.teams.red.roster.name;
    const blueTeamName = matchData.teams.blue.roster.name;
    const isJstCarry = redTeamName === "JstCarry";
    const displayTeamName = isJstCarry ? blueTeamName : redTeamName;

    const redRoundsWon = matchData.teams.red.rounds_won;
    const redRoundsLost = matchData.teams.red.rounds_lost;
    const blueRoundsWon = matchData.teams.blue.rounds_won;
    const blueRoundsLost = matchData.teams.blue.rounds_lost;
    const roundsWon = isJstCarry ? redRoundsWon : blueRoundsWon;
    const roundsLost = isJstCarry ? redRoundsLost : blueRoundsLost;
    const matchScore = `${roundsWon} : ${roundsLost}`;

    const matchBox = document.createElement("div");
    matchBox.classList.add("match-box");

    if ((isJstCarry && redRoundsWon > redRoundsLost) || (!isJstCarry && blueRoundsWon > blueRoundsLost)) {
      const victoryDefeatElement = document.createElement("div");
      victoryDefeatElement.classList.add("victory-defeat");
      victoryDefeatElement.textContent = "VICTORY";
      matchBox.appendChild(victoryDefeatElement);
    } else {
      const victoryDefeatElement = document.createElement("div");
      victoryDefeatElement.classList.add("victory-defeat");
      victoryDefeatElement.textContent = "DEFEAT";
      matchBox.appendChild(victoryDefeatElement);
    }

    const jstCarryTeamNameElement = document.createElement("div");
    jstCarryTeamNameElement.classList.add("team-name", "left");
    jstCarryTeamNameElement.textContent = "JstCarry";

    const displayTeamNameElement = document.createElement("div");
    displayTeamNameElement.classList.add("team-name", "right");
    displayTeamNameElement.textContent = displayTeamName;

    const scoreElement = document.createElement("div");
    scoreElement.classList.add("score");
    scoreElement.textContent = matchScore;

    const pointsBefore = match.points_before; // Get the points_after value

    const pointsAfter = match.points_after; // Get the points_after value
    const pointsElement = document.createElement("div");
    pointsElement.classList.add("points");
    pointsElement.textContent = `${pointsBefore} ➜ ${pointsAfter} points`;




    matchBox.appendChild(jstCarryTeamNameElement);
    matchBox.appendChild(scoreElement);

    matchBox.appendChild(displayTeamNameElement);


    matchHistoryElement.appendChild(matchBox);
    scoreElement.appendChild(pointsElement); // Add the points element to the match box
  });
}

fetchAndDisplayMatches();
body {
  min-width: 540px;
  font-family: 'Montserrat', sans-serif;
}

.match-box {
  background-color: #1B1C1E;
  border-radius: 25px;
  padding: 0;
  margin: 10px 0;
  height: 120px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  color: white;
  padding: 0 20px;
  position: relative;
}

.victory-defeat {
  position: absolute;
  opacity: 0.2;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 80px;
  font-weight: bold;
  color: white;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 25px;
}

.team-name {
  text-align: left;
}

.team-name.left {
  flex: 1;
}

.team-name.right {
  text-align: right;
  flex: 1;
}

.score {
  font-weight: bold;
  font-size: 50px;
  text-align: center;
  flex: 1;
}

.points {
  position: flex;
  text-align: center;
  font-size: 18px;
  opacity: 0.7;
}
```
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Valorant Match History</title>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400&display=swap" rel="stylesheet">
</head>

<body>
  <div id="match-history"></div>

</body>

</html>

推荐答案

问题是您没有按排序顺序处理结果,因为您在forEach()循环中调用了一个异步函数.

fetchMatchData(matchId)返回的所有promise 数组,然后使用Promise.all()按顺序处理它们.

async function fetchMatchData(matchId) {
  const matchUrl = `https://api.henrikdev.xyz/valorant/v2/match/${matchId}`;
  const response = await fetch(matchUrl);
  const matchData = await response.json();
  return matchData.data;
}

async function fetchAndDisplayMatches() {
  const historyUrl = "https://api.henrikdev.xyz/valorant/v1/premier/70921669-f22e-4a59-9e26-f19664a1fa20/history";
  const response = await fetch(historyUrl);
  const historyData = await response.json();
  const leagueMatches = historyData.data.league_matches;

  // Sort the matches array based on points_before in descending order
  leagueMatches.sort((a, b) => b.points_before - a.points_before);

  const matchHistoryElement = document.getElementById("match-history");

  const resultPromises = leagueMatches.map(match => fetchMatchData(match.id));
  (await Promise.all(resultPromises)).forEach((matchData, i) => {

    let match = leagueMatches[i];
    const redTeamName = matchData.teams.red.roster.name;
    const blueTeamName = matchData.teams.blue.roster.name;
    const isJstCarry = redTeamName === "JstCarry";
    const displayTeamName = isJstCarry ? blueTeamName : redTeamName;

    const redRoundsWon = matchData.teams.red.rounds_won;
    const redRoundsLost = matchData.teams.red.rounds_lost;
    const blueRoundsWon = matchData.teams.blue.rounds_won;
    const blueRoundsLost = matchData.teams.blue.rounds_lost;
    const roundsWon = isJstCarry ? redRoundsWon : blueRoundsWon;
    const roundsLost = isJstCarry ? redRoundsLost : blueRoundsLost;
    const matchScore = `${roundsWon} : ${roundsLost}`;

    const matchBox = document.createElement("div");
    matchBox.classList.add("match-box");

    if ((isJstCarry && redRoundsWon > redRoundsLost) || (!isJstCarry && blueRoundsWon > blueRoundsLost)) {
      const victoryDefeatElement = document.createElement("div");
      victoryDefeatElement.classList.add("victory-defeat");
      victoryDefeatElement.textContent = "VICTORY";
      matchBox.appendChild(victoryDefeatElement);
    } else {
      const victoryDefeatElement = document.createElement("div");
      victoryDefeatElement.classList.add("victory-defeat");
      victoryDefeatElement.textContent = "DEFEAT";
      matchBox.appendChild(victoryDefeatElement);
    }

    const jstCarryTeamNameElement = document.createElement("div");
    jstCarryTeamNameElement.classList.add("team-name", "left");
    jstCarryTeamNameElement.textContent = "JstCarry";

    const displayTeamNameElement = document.createElement("div");
    displayTeamNameElement.classList.add("team-name", "right");
    displayTeamNameElement.textContent = displayTeamName;

    const scoreElement = document.createElement("div");
    scoreElement.classList.add("score");
    scoreElement.textContent = matchScore;

    const pointsBefore = match.points_before; // Get the points_after value

    const pointsAfter = match.points_after; // Get the points_after value
    const pointsElement = document.createElement("div");
    pointsElement.classList.add("points");
    pointsElement.textContent = `${pointsBefore} ➜ ${pointsAfter} points`;

    matchBox.appendChild(jstCarryTeamNameElement);
    matchBox.appendChild(scoreElement);

    matchBox.appendChild(displayTeamNameElement);

    matchHistoryElement.appendChild(matchBox);
    scoreElement.appendChild(pointsElement); // Add the points element to the match box
  });
}

fetchAndDisplayMatches();
body {
  min-width: 540px;
  font-family: 'Montserrat', sans-serif;
}

.match-box {
  background-color: #1B1C1E;
  border-radius: 25px;
  padding: 0;
  margin: 10px 0;
  height: 120px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  color: white;
  padding: 0 20px;
  position: relative;
}

.victory-defeat {
  position: absolute;
  opacity: 0.2;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 80px;
  font-weight: bold;
  color: white;
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 25px;
}

.team-name {
  text-align: left;
}

.team-name.left {
  flex: 1;
}

.team-name.right {
  text-align: right;
  flex: 1;
}

.score {
  font-weight: bold;
  font-size: 50px;
  text-align: center;
  flex: 1;
}

.points {
  position: flex;
  text-align: center;
  font-size: 18px;
  opacity: 0.7;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Valorant Match History</title>
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400&display=swap" rel="stylesheet">
</head>

<body>
  <div id="match-history"></div>

</body>

</html>

Javascript相关问答推荐

使用useParams路由失败

django无法解析余数:[0] from carray[0]'

docx.js:如何在客户端使用文档修补程序

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

Msgraph用户邀请自定义邮箱模板

点击按钮一次有文本出现和褪色,而不是点击两次?(html,CSS,JavaScript)

如何禁用附加图标点击的v—自动完成事件

实现JS代码更改CSS元素

虚拟滚动实现使向下滚动可滚动到末尾

使用ThreeJ渲染的形状具有抖动/模糊的边缘

确保函数签名中的类型安全:匹配值

第三方包不需要NODE_MODULES文件夹就可以工作吗?

为什么在函数中添加粒子的速率大于删除粒子的速率?

如何为仅有数据可用的点显示X轴标签?

为什么延迟在我的laravel项目中不起作用?

处理app.param()中的多个参数

Cherrio JS返回父div的所有图像SRC

无法使用npm install安装react-dom、react和next

如何在Reaction中设置缺省值, Select 下拉列表,动态追加剩余值?

如何在加载页面时使锚定标记成为焦点