对嵌套的javascript数组/对象进行排序[关闭]

问题描述 投票:0回答:1

我有一个像这样的Javascript多级对象:

{
    "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
                }
            ]
        }
    }
}

我如何根据每种游戏类型的总得分对该数组进行排序。例如,我想对网球的最高总分进行排序。

由于我对 javascript 相当陌生,所以我不知道到底从哪里开始。希望你能帮助我

javascript sorting object multidimensional-array
1个回答
0
投票

您必须展平数据,然后按分数对记录进行排序。

const records = flattenToRecords(getRawData())
  .toSorted((a, b) =>
    /* Highest score */
    (b.score - a.score) ||
    /* Most recent */
    (normalizeDate(b.date).localeCompare(normalizeDate(a.date))) ||
    /* Player A-Z */
    (a.player.localeCompare(b.player))
  );

console.log(...records.map(JSON.stringify));

function flattenToRecords(dict) {
  const results = [];
  for (let sport in dict) {
    const playerDict = dict[sport];
    for (let player in playerDict) {
      const dataDict = playerDict[player];
      const totalScore = dataDict.Total_score;
      for (let game of dataDict.Played_games) {
        for (let date in game) {
          const score = game[date];
          results.push({ date, score, player, sport, totalScore });
        }
      }
    }
  }
  return results;
}

function normalizeDate(date) {
  return date.split('/').map(d => d.padStart(2, '0')).reverse().join('-');
}

function getRawData () {
  return {
    "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 }
        ]
      }
    }
  };
};
.as-console-row-code { font-size: smaller !important; }
.as-console-wrapper { top: 0; max-height: 100% !important; }

© www.soinside.com 2019 - 2024. All rights reserved.