简体   繁体   English

如何实现净胜球计算以实现决胜局?

[英]how to implement the goal difference calculation to achieve a tiebreaker?

I work with the following data set我使用以下数据集

const data = [
  {
    team: {
      id: "1018",
      title: "Team 1",
      permalink: "http://localhost/app/teams/team-1/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-1.png",
    },
    games_played: 1,
    won_games: 1,
    tide_games: 0,
    lost_games: 0,
    goals_for: 3,
    goals_against: 1,
    free_play_score: 1,
    points: 4,
  },
  {
    team: {
      id: "211",
      title: "Team 2",
      permalink: "http://localhost/app/teams/team-2/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-2.png",
    },
    games_played: 2,
    won_games: 0,
    tide_games: 1,
    lost_games: 1,
    goals_for: 1,
    goals_against: 3,
    free_play_score: 1,
    points: 1,
  },
  {
    team: {
      id: "2098",
      title: "Team 3",
      permalink: "http://localhost/app/teams/team-3/",
      logo: "http://localhost/app/wp-content/uploads/2020/07/team-3.png",
    },
    games_played: 1,
    won_games: 0,
    tide_games: 1,
    lost_games: 0,
    goals_for: 0,
    goals_against: 0,
    free_play_score: 0,
    points: 0,
  },
  {
    team: {
      id: "196",
      title: "Team 4",
      permalink: "http://localhost/app/teams/team-4/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-4.png",
    },
    games_played: 1,
    won_games: 0,
    tide_games: 1,
    lost_games: 0,
    goals_for: 1,
    goals_against: 1,
    free_play_score: 0,
    points: 1,
  },
  {
    team: {
      id: "250",
      title: "Team 5",
      permalink: "http://localhost/app/teams/team-5/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-5.png",
    },
    games_played: 2,
    won_games: 2,
    tide_games: 0,
    lost_games: 0,
    goals_for: 4,
    goals_against: 3,
    free_play_score: 0,
    points: 2,
  },
  {
    team: {
      id: "147",
      title: "Team 6",
      permalink: "http://localhost/app/teams/team-6/",
      logo: "http://localhost/app/wp-content/uploads/2020/07/team-5.png",
    },
    games_played: 2,
    won_games: 2,
    tide_games: 0,
    lost_games: 0,
    goals_for: 5,
    goals_against: 1,
    free_play_score: 0,
    points: 2,
  },
];

I need to sort them by points, so that I can later print standings我需要按积分对它们进行排序,以便我以后可以打印积分榜

data.sort((a, b) => b.points - a.points);

But then I get two tie scenarios, between the teams that have two points and those that have one point, I must here use Goal difference to order them,但是后来我得到了两个平局场景,在两分的球队和一分的球队之间,我必须在这里使用目标差异来排列它们,

Goal difference consists of目标差异包括

Goal difference (or points difference) is calculated as the number of goals (or points) scored in all league matches minus the number of goals or points conceded进球数(或积分差)计算为所有联赛的进球数(或积分数)减去进球数或失分数

In my case, first I thought about obtaining the difference goals of each team, for this I try the following就我而言,首先我想到了获得每个团队的不同目标,为此我尝试以下

const clone = data.concat().map((entry) => ({
  ...entry,
  team: { ...entry.team },
  goal_difference: entry.goals_for - entry.goals_against,
}));

The goal_difference property is added to each team, but from now on I don't know how to proceed, to achieve the positions of teams that do not have a tie are ordered and respected.每个队都添加了goal_difference属性,但是从现在开始我不知道如何进行,以实现没有平局的球队的位置被排序和尊重。 I appreciate your suggestions我很欣赏你的建议

** Update 0 ** ** 更新 0 **

The expected output should be预期的输出应该是

const data = [
  {
    team: {
      id: "1018",
      title: "Team 1",
      permalink: "http://localhost/app/teams/team-1/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-1.png",
    },
    games_played: 1,
    won_games: 1,
    tide_games: 0,
    lost_games: 0,
    goals_for: 3,
    goals_against: 1,
    free_play_score: 1,
    points: 4,
    goal_difference: 2,
  },
  {
    team: {
      id: "147",
      title: "Team 6",
      permalink: "http://localhost/app/teams/team-6/",
      logo: "http://localhost/app/wp-content/uploads/2020/07/team-5.png",
    },
    games_played: 2,
    won_games: 2,
    tide_games: 0,
    lost_games: 0,
    goals_for: 5,
    goals_against: 1,
    free_play_score: 0,
    points: 2,
    goal_difference: 4,
  },
  {
    team: {
      id: "250",
      title: "Team 5",
      permalink: "http://localhost/app/teams/team-5/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-5.png",
    },
    games_played: 2,
    won_games: 2,
    tide_games: 0,
    lost_games: 0,
    goals_for: 4,
    goals_against: 3,
    free_play_score: 0,
    points: 2,
    goal_difference: 1,
  },
  {
    team: {
      id: "196",
      title: "Team 4",
      permalink: "http://localhost/app/teams/team-4/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-4.png",
    },
    games_played: 1,
    won_games: 0,
    tide_games: 1,
    lost_games: 0,
    goals_for: 1,
    goals_against: 1,
    free_play_score: 0,
    points: 1,
    goal_difference: 0,
  },
  {
    team: {
      id: "211",
      title: "Team 2",
      permalink: "http://localhost/app/teams/team-2/",
      logo: "http://localhost/app/wp-content/uploads/2020/08/team-2.png",
    },
    games_played: 2,
    won_games: 0,
    tide_games: 1,
    lost_games: 1,
    goals_for: 1,
    goals_against: 3,
    free_play_score: 1,
    points: 1,
    goal_difference: -2,
  },
  {
    team: {
      id: "2098",
      title: "Team 3",
      permalink: "http://localhost/app/teams/team-3/",
      logo: "http://localhost/app/wp-content/uploads/2020/07/team-3.png",
    },
    games_played: 1,
    won_games: 0,
    tide_games: 1,
    lost_games: 0,
    goals_for: 0,
    goals_against: 0,
    free_play_score: 0,
    points: 0,
    goal_difference: 0,
  },
];

You simply need to detect when points are equal and then sort on goal difference instead.您只需要检测点何时相等,然后根据净胜球数进行排序。 You can do this inline without adding to the object, if you prefer:如果您愿意,您可以在不添加对象的情况下内联执行此操作:

data.sort((a, b) => b.points == a.points ? 
(b.goals_for - b.goals_against) - (a.goals_for - b.goals_against) : 
b.points - a.points );

You could first map over you data to add goals_difference property to each object and then use sort method to first sort by points and then by goals difference if the points are equal.您可以首先map您的数据以将目标goals_difference属性添加到每个对象,然后使用sort方法首先按点排序,然后在点相等时按目标差异排序。

 const data = [{"team":{"id":"1018","title":"Team 1","permalink":"http://localhost/app/teams/team-1/","logo":"http://localhost/app/wp-content/uploads/2020/08/team-1.png"},"games_played":1,"won_games":1,"tide_games":0,"lost_games":0,"goals_for":3,"goals_against":1,"free_play_score":1,"points":4},{"team":{"id":"211","title":"Team 2","permalink":"http://localhost/app/teams/team-2/","logo":"http://localhost/app/wp-content/uploads/2020/08/team-2.png"},"games_played":2,"won_games":0,"tide_games":1,"lost_games":1,"goals_for":1,"goals_against":3,"free_play_score":1,"points":1},{"team":{"id":"2098","title":"Team 3","permalink":"http://localhost/app/teams/team-3/","logo":"http://localhost/app/wp-content/uploads/2020/07/team-3.png"},"games_played":1,"won_games":0,"tide_games":1,"lost_games":0,"goals_for":0,"goals_against":0,"free_play_score":0,"points":0},{"team":{"id":"196","title":"Team 4","permalink":"http://localhost/app/teams/team-4/","logo":"http://localhost/app/wp-content/uploads/2020/08/team-4.png"},"games_played":1,"won_games":0,"tide_games":1,"lost_games":0,"goals_for":1,"goals_against":1,"free_play_score":0,"points":1},{"team":{"id":"250","title":"Team 5","permalink":"http://localhost/app/teams/team-5/","logo":"http://localhost/app/wp-content/uploads/2020/08/team-5.png"},"games_played":2,"won_games":2,"tide_games":0,"lost_games":0,"goals_for":4,"goals_against":3,"free_play_score":0,"points":2},{"team":{"id":"147","title":"Team 6","permalink":"http://localhost/app/teams/team-6/","logo":"http://localhost/app/wp-content/uploads/2020/07/team-5.png"},"games_played":2,"won_games":2,"tide_games":0,"lost_games":0,"goals_for":5,"goals_against":1,"free_play_score":0,"points":2}] const result = data .map(({ goals_for: gf, goals_against: ga, ...rest }) => ({ ...rest, goal_difference: gf - ga})) .sort((a, b) => b.points - a.points || b.goal_difference - a.goal_difference) console.log(result)

Try:尝试:

 const data = [ { team: { id: "1018", title: "Team 1", permalink: "http://localhost/app/teams/team-1/", logo: "http://localhost/app/wp-content/uploads/2020/08/team-1.png", }, games_played: 1, won_games: 1, tide_games: 0, lost_games: 0, goals_for: 3, goals_against: 1, free_play_score: 1, points: 4, }, { team: { id: "211", title: "Team 2", permalink: "http://localhost/app/teams/team-2/", logo: "http://localhost/app/wp-content/uploads/2020/08/team-2.png", }, games_played: 2, won_games: 0, tide_games: 1, lost_games: 1, goals_for: 1, goals_against: 3, free_play_score: 1, points: 1, }, { team: { id: "2098", title: "Team 3", permalink: "http://localhost/app/teams/team-3/", logo: "http://localhost/app/wp-content/uploads/2020/07/team-3.png", }, games_played: 1, won_games: 0, tide_games: 1, lost_games: 0, goals_for: 0, goals_against: 0, free_play_score: 0, points: 0, }, { team: { id: "196", title: "Team 4", permalink: "http://localhost/app/teams/team-4/", logo: "http://localhost/app/wp-content/uploads/2020/08/team-4.png", }, games_played: 1, won_games: 0, tide_games: 1, lost_games: 0, goals_for: 1, goals_against: 1, free_play_score: 0, points: 1, }, { team: { id: "250", title: "Team 5", permalink: "http://localhost/app/teams/team-5/", logo: "http://localhost/app/wp-content/uploads/2020/08/team-5.png", }, games_played: 1, won_games: 1, tide_games: 0, lost_games: 0, goals_for: 4, goals_against: 3, free_play_score: 0, points: 1, }, { team: { id: "147", title: "Team 6", permalink: "http://localhost/app/teams/team-6/", logo: "http://localhost/app/wp-content/uploads/2020/07/team-5.png", }, games_played: 1, won_games: 1, tide_games: 0, lost_games: 0, goals_for: 5, goals_against: 1, free_play_score: 0, points: 1, }, ]; function sortArray(a, b) { var aPoints = a.points; var bPoints = b.points; var aDifference = a.goals_for - a.goals_against; var bDifference = b.goals_for - b.goals_against; var r; if (aPoints < bPoints) { r = 1; } else if (aPoints > bPoints) { r = -1; } else { if (aDifference < bDifference) { r = 1; } else if (aDifference > bDifference) { r = -1; } else { r = 0; } } return r; } data.sort(sortArray); console.log(data);

This is a custom sort that does a normal descending sort (ie, team with most points is first) except that, where the points match, it then checks the goal differences and sorts on that as well这是一种自定义排序,它执行正常的降序排序(即,得分最多的球队排在第一位),但在得分匹配的情况下,它会检查目标差异并对其进行排序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM