You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按各运动类型总分对嵌套JavaScript对象进行排序?

按运动类型的总分排序JavaScript对象的方法

因为JavaScript普通对象本身不支持直接排序,我们需要先把对象转换成可排序的数组,定义排序规则后再转回对象,具体步骤如下:

1. 准备原始数据

先把你的数据存为一个变量:

const sportsData = {
    "tennis": {
        "John": { "Total_score": 329, "Played_games": [{ "31/12/2024": 151 }, { "25/12/2024": 178 }] },
        "Klaas": { "Total_score": 188, "Played_games": [{ "10/11/2024": 188 }] }
    },
    "Hockey": {
        "Piet": { "Total_score": 166, "Played_games": [{ "01/12/2024": 166 }] }
    },
    "Football": {
        "John": { "Total_score": 133, "Played_games": [{ "06/12/2024": 133 }] }
    },
    "Baseball": {
        "Marie": { "Total_score": 121, "Played_games": [{ "24/12/2024": 121 }] }
    },
    "Kitesurfing": {
        "Peter": { "Total_score": 185, "Played_games": [{ "11/12/2024": 185 }] }
    }
};

2. 将对象转为可排序的数组

用Object.entries()把对象转成[运动名称, 选手数据]的键值对数组,这是排序的前提:

const sportsEntries = Object.entries(sportsData);
// 输出示例:[["tennis", {John: {...}, Klaas: {...}}], ["Hockey", {Piet: {...}}], ...]

3. 定义排序规则

按运动的最高选手总分排序(比如网球取John的329)

提取每个运动下所有选手的分数,取最大值作为排序依据,这里按降序(从高到低)排列:

sportsEntries.sort((a, b) => {
    // 提取当前运动所有选手的总分
    const scoresA = Object.values(a[1]).map(player => player.Total_score);
    const scoresB = Object.values(b[1]).map(player => player.Total_score);
    // 取最高分
    const maxScoreA = Math.max(...scoresA);
    const maxScoreB = Math.max(...scoresB);
    // 降序排序:b的分数减a的分数;升序则反过来写a - b
    return maxScoreB - maxScoreA;
});

按运动的所有选手总分之和排序

如果需要按该运动所有选手的总分总和排序,把取最大值换成求和即可:

sportsEntries.sort((a, b) => {
    const totalScoreA = Object.values(a[1]).reduce((sum, player) => sum + player.Total_score, 0);
    const totalScoreB = Object.values(b[1]).reduce((sum, player) => sum + player.Total_score, 0);
    return totalScoreB - totalScoreA;
});

4. 将排序后的数组转回对象

用Object.fromEntries()把排序后的数组转成新的有序对象:

const sortedSportsData = Object.fromEntries(sportsEntries);
console.log(sortedSportsData);

完整示例代码

const sportsData = {
    "tennis": {
        "John": { "Total_score": 329, "Played_games": [{ "31/12/2024": 151 }, { "25/12/2024": 178 }] },
        "Klaas": { "Total_score": 188, "Played_games": [{ "10/11/2024": 188 }] }
    },
    "Hockey": {
        "Piet": { "Total_score": 166, "Played_games": [{ "01/12/2024": 166 }] }
    },
    "Football": {
        "John": { "Total_score": 133, "Played_games": [{ "06/12/2024": 133 }] }
    },
    "Baseball": {
        "Marie": { "Total_score": 121, "Played_games": [{ "24/12/2024": 121 }] }
    },
    "Kitesurfing": {
        "Peter": { "Total_score": 185, "Played_games": [{ "11/12/2024": 185 }] }
    }
};

// 转成键值对数组
const sportsEntries = Object.entries(sportsData);

// 按运动的最高选手总分降序排序
sportsEntries.sort((a, b) => {
    const maxA = Math.max(...Object.values(a[1]).map(p => p.Total_score));
    const maxB = Math.max(...Object.values(b[1]).map(p => p.Total_score));
    return maxB - maxA;
});

// 转回对象
const sortedData = Object.fromEntries(sportsEntries);

console.log(sortedData);

内容的提问来源于stack exchange,提问作者Bas van Ingen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:35:39