Javascript 如何实现目标差计算以实现平局?
我使用以下数据集Javascript 如何实现目标差计算以实现平局?,javascript,Javascript,我使用以下数据集 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",
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,
},
];
我需要按点数对它们进行排序,以便以后打印排名
data.sort((a, b) => b.points - a.points);
但是我得到了两个平局的场景,在两分球队和一分球队之间,我必须在这里对他们进行排序
目标差异包括
目标差(或分数差)计算为
所有联赛中的进球(或分数)减去
失球或失分
在我的例子中,首先我想获得每个团队的不同目标,为此我尝试以下方法
const clone = data.concat().map((entry) => ({
...entry,
team: { ...entry.team },
goal_difference: entry.goals_for - entry.goals_against,
}));
每个团队都添加了goal\u difference
属性,但从现在起,我不知道如何继续,以实现没有平局的团队的位置得到有序和尊重。我感谢你的建议
**更新0**
预期输出应为
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,
},
];
尝试:
const数据=[
{
小组:{
id:“1018”,
标题:“第一队”,
permalink:“http://localhost/app/teams/team-1/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-1.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:3,
目标:1,,
免费游戏分数:1,
要点:4,
},
{
小组:{
身份证号码:“211”,
标题:“第二队”,
permalink:“http://localhost/app/teams/team-2/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-2.png",
},
你玩的游戏:2,
赢得比赛:0,
潮水小游戏:1,
失利游戏:1,
目标:1,
目标:3,
免费游戏分数:1,
要点:一,,
},
{
小组:{
身份证号码:“2098”,
标题:“第三队”,
permalink:“http://localhost/app/teams/team-3/",
标志:“http://localhost/app/wp-content/uploads/2020/07/team-3.png",
},
你玩的游戏:1,
赢得比赛:0,
潮水小游戏:1,
输掉的游戏:0,
目标:0,
目标:0,
免费游戏分数:0,
分数:0,,
},
{
小组:{
id:“196”,
标题:“第四队”,
permalink:“http://localhost/app/teams/team-4/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-4.png",
},
你玩的游戏:1,
赢得比赛:0,
潮水小游戏:1,
输掉的游戏:0,
目标:1,
目标:1,,
免费游戏分数:0,
要点:一,,
},
{
小组:{
id:“250”,
标题:“第5队”,
permalink:“http://localhost/app/teams/team-5/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-5.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:4,
目标:3,
免费游戏分数:0,
要点:一,,
},
{
小组:{
id:“147”,
标题:“第6队”,
permalink:“http://localhost/app/teams/team-6/",
标志:“http://localhost/app/wp-content/uploads/2020/07/team-5.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:5,
目标:1,,
免费游戏分数:0,
要点:一,,
},
];
函数排序(a,b){
var aPoints=a点;
var b点=b点;
var a差异=a.目标针对-a.目标针对;
var b差异=b.目标针对-b.目标针对;
var-r;
如果(A点点){
r=-1;
}否则{
if(a差异差异){
r=-1;
}否则{
r=0;
}
}
返回r;
}
数据排序(sortArray);
控制台日志(数据)代码>尝试:
const数据=[
{
小组:{
id:“1018”,
标题:“第一队”,
permalink:“http://localhost/app/teams/team-1/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-1.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:3,
目标:1,,
免费游戏分数:1,
要点:4,
},
{
小组:{
身份证号码:“211”,
标题:“第二队”,
permalink:“http://localhost/app/teams/team-2/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-2.png",
},
你玩的游戏:2,
赢得比赛:0,
潮水小游戏:1,
失利游戏:1,
目标:1,
目标:3,
免费游戏分数:1,
要点:一,,
},
{
小组:{
身份证号码:“2098”,
标题:“第三队”,
permalink:“http://localhost/app/teams/team-3/",
标志:“http://localhost/app/wp-content/uploads/2020/07/team-3.png",
},
你玩的游戏:1,
赢得比赛:0,
潮水小游戏:1,
输掉的游戏:0,
目标:0,
目标:0,
免费游戏分数:0,
分数:0,,
},
{
小组:{
id:“196”,
标题:“第四队”,
permalink:“http://localhost/app/teams/team-4/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-4.png",
},
你玩的游戏:1,
赢得比赛:0,
潮水小游戏:1,
输掉的游戏:0,
目标:1,
目标:1,,
免费游戏分数:0,
要点:一,,
},
{
小组:{
id:“250”,
标题:“第5队”,
permalink:“http://localhost/app/teams/team-5/",
标志:“http://localhost/app/wp-content/uploads/2020/08/team-5.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:4,
目标:3,
免费游戏分数:0,
要点:一,,
},
{
小组:{
id:“147”,
标题:“第6队”,
permalink:“http://localhost/app/teams/team-6/",
标志:“http://localhost/app/wp-content/uploads/2020/07/team-5.png",
},
你玩的游戏:1,
赢得比赛:1,
潮水小游戏:0,
输掉的游戏:0,
目标:5,
目标:1,,
免费游戏分数:0,
要点:一,,
},
];
函数排序(a,b){
var aPoints=a点;
var b点=b点;
var a差异=a.目标针对-a.目标针对;
var b差异=b.目标针对-b.目标针对;
var-r;
如果(A点data.sort((a, b) => b.points == a.points ?
(b.goals_for - b.goals_against) - (a.goals_for - b.goals_against) :
b.points - a.points );