按日期对对象数组进行排序,其中某些日期为空

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

我有一个名为 steamData 的巨大对象数组,如下所示:

 {
        "sid": 1690,
        "store_url": "https://store.steampowered.com/app/1690",
        "store_promo_url": null,
        "store_uscore": 69,
        "published_store": "2006-10-16",
        "published_meta": "2006-10-16",
        "published_stsp": "2006-10-16",
        "published_hltb": "2020-04-18",
        "published_igdb": "2006-09-01",
        "image": "https://steamcdn-a.akamaihd.net/steam/apps/1690/header.jpg",
        "name": "Space Empires V",
        "description": "Space Empires V is the latest edition in the Space Empires series. This new chapter completely updates the UI and takes ...",
        "full_price": 1999,
        "current_price": 499,
        "discount": 75,
        "platforms": "WIN",
        "developers": "Malfador Machinations",
        "publishers": "Strategy First",
        "languages": "English",
        "voiceovers": null,
        "categories": "Single-player,Multi-player",
        "genres": "Strategy",
        "tags": "Strategy,4X,Space,Sci-fi,Turn-Based,Singleplayer,Turn-Based Strategy,Multiplayer",
        "achievements": null,
        "gfq_url": "https://gamefaqs.gamespot.com/pc/927958-space-empires-v?ftag=MCD-06-10aaa1f",
        "gfq_difficulty": "Just Right-Tough",
        "gfq_difficulty_comment": "<a href=\"/games/rankings?platform=19&amp;genre=45&amp;list_type=diff&amp;dlc=1&amp;page=22&amp;game_id=927958&amp;min_votes=2#1114\"><b>#1114</b></a> hardest PC strategy game (<a href=\"/games/rankings?platform=19&amp;list_type=diff&amp;dlc=1&amp;page=122&amp;game_id=927958&amp;min_votes=2#6145\"><b>#6145</b></a> on PC, <b>#24997</b> overall)",
        "gfq_rating": 3.54,
        "gfq_rating_comment": "<a href=\"/games/rankings?platform=19&amp;genre=45&amp;list_type=rate&amp;view_type=1&amp;dlc=1&amp;page=16&amp;game_id=927958&amp;min_votes=2#818\"><b>#818</b></a> lowest rated PC strategy game (<a href=\"/games/rankings?platform=19&amp;list_type=rate&amp;view_type=1&amp;dlc=1&amp;page=163&amp;game_id=927958&amp;min_votes=2#8199\"><b>#8199</b></a> on PC, <b>#33932</b> overall)",
        "gfq_length": 68.6,
        "gfq_length_comment": "<a href=\"/games/rankings?platform=19&amp;genre=45&amp;list_type=time&amp;dlc=1&amp;page=2&amp;game_id=927958&amp;min_votes=2#119\"><b>#119</b></a> longest PC strategy game (<a href=\"/games/rankings?platform=19&amp;list_type=time&amp;dlc=1&amp;page=12&amp;game_id=927958&amp;min_votes=2#618\"><b>#618</b></a> on PC, <b>#1757</b> overall)",
        "stsp_owners": 75000,
        "stsp_mdntime": null,
        "hltb_url": "https://howlongtobeat.com/game?id=8846",
        "hltb_single": 10,
        "hltb_complete": null,
        "meta_url": "https://www.metacritic.com/game/pc/space-empires-v?ftag=MCD-06-10aaa1f",
        "meta_score": 68,
        "meta_uscore": 77,
        "grnk_score": 65,
        "igdb_url": "https://www.igdb.com/games/space-empires-v",
        "igdb_single": null,
        "igdb_complete": null,
        "igdb_score": 65,
        "igdb_uscore": 50,
        "igdb_popularity": 1.89
    },

我正在尝试按其published_meta日期从最新到最旧的顺序对该数组进行排序:

 if (!loading) {
    // Sort from oldest to youngest
    steamData.sort((a, b) => (b)?.published_meta.localeCompare((a)?.published_meta));

    // Get today as milliseconds
    let today = new Date().toLocaleDateString('en-CA');
    let todayMS = Date.parse(today);

    // Get index of closest element to today
    let closestIndex = steamData.reduce((acc, obj, index) => {
      let diff = Math.abs(Date.parse(obj?.published_meta) - todayMS);
      return acc.diff > diff ? { diff, index } : acc;
    }, { diff: Infinity, index: null }).index;

    // Move closest element to start of array
    steamData.unshift(newTrending.splice(closestIndex, 1));
  }

我不断遇到错误“无法读取 null 的属性(读取“localeCompare”)”

一些对象的发布日期为 null(published_meta 似乎是其中最少的)。我认为可选链接可以解决这个问题,但事实并非如此。

如何对要使用的 steamData 数组进行排序?

javascript arrays sorting
2个回答
0
投票

您还需要

?
上的
published_meta
published_meta?.localeCompare
。无法在 null 上调用
localeCompare

这是第一步。我怀疑使用

null
进行排序会导致意外行为。


0
投票
  1. 只需用假字符串填充空值
  2. 不要使用
    localeCompare
    ,因为它非常慢,每次都会创建
    Intl.Collator
    。您的字符只是数字,与
    <>
    运算符进行比较或仅创建
    Intl.Collator
    一次。

const items = [{"published_meta": null}, {"published_meta": "2006-10-16"}, {"published_meta": "2006-10-15"}];

items.sort(({published_meta: a}, {published_meta: b}) => (a ??= '0', b ??= '0', a > b ? -1 : a < b ? 1 : 0));

console.log(items);

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