我使用以下数据集
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_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,
},
];发布于 2020-09-18 16:56:24
你只需要检测分数是相等的,然后对目标差进行排序。您可以在不添加对象的情况下内联完成此操作,如果您愿意的话:
data.sort((a, b) => b.points == a.points ?
(b.goals_for - b.goals_against) - (a.goals_for - b.goals_against) :
b.points - a.points );发布于 2020-09-18 17:00:52
您可以首先对数据进行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)
发布于 2020-09-18 16:55:45
尝试:
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);
这是一种自定义排序,它执行一个正常的降序排序(即,得分最多的球队首先进行排序),但在得分匹配的地方,它会检查目标的差异并对其进行排序。
https://stackoverflow.com/questions/63959672
复制相似问题