【问题标题】:Sort array to new 2d array [closed]将数组排序为新的二维数组[关闭]
【发布时间】:2020-11-22 18:15:51
【问题描述】:

[
  {
    "id": 3401,
    "league": {
      "id": 4140,
      "image_url": "pngurl.png",
      "modified_at": "2019-02-19T15:29:07Z",
      "name": "League Cup",
      "slug": "league-slug",
      "url": null
    }
  }
]

我在数组中有很多这样的对象。我如何对数组进行排序/格式化,这样我得到了一个新数组/字典,其中对象按"league" 中的"id" 值排序,例如4140。现在我有一个普通数组,但我想要一些按键值排序的东西,比如 4140:[来自数组的对象,联盟 ID 为 4140] 或排序的二维数组[[array with all objects containing league with value "id" of 4140], [array with objects with league value id 4141], etc..]。我需要使用map 或类似的东西吗?我对 Javascript 很陌生。

这是我期望在代码中得到的。

//THIS is what I want to get in the end...
[
  {
    "league": "League Cup",
    "tournaments": [
      {
        "id": 3402,
        "league": {
          "id": 4141,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3403,
        "league": {
          "id": 4141,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      }
    ]
  },
  {
    "league": "League Cup 2",
    "tournaments": [
      {
        "id": 3401,
        "league": {
          "id": 4145,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup 2",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3405,
        "league": {
          "id": 4145,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup 2",
          "slug": "league-slug",
          "url": null
        }
      }
    ]
  }
]

【问题讨论】:

  • 请添加更多数据、想要的结果您的尝试。
  • 您可以根据需要添加一小部分代码吗?我们需要更多关于它的信息:)

标签: javascript arrays typescript


【解决方案1】:

您需要遍历它并根据您的联赛 ID 将它们分组到一个新对象中。

例子:

const output = {};
const input = [
  {
    "id": 1,
    "league": {
      "id": 7,
      "image_url": "pngurl.png",
      "modified_at": "2019-02-19T15:29:07Z",
      "name": "League Cup",
      "slug": "league-slug",
      "url": null
    }
  },
  {
    "id": 2,
    "league": {
      "id": 7,
      "image_url": "pngurl.png",
      "modified_at": "2019-02-19T15:29:07Z",
      "name": "League Cup",
      "slug": "league-slug",
      "url": null
    }
  },
  {
    "id": 3,
    "league": {
      "id": 8,
      "image_url": "pngurl.png",
      "modified_at": "2019-02-19T15:29:07Z",
      "name": "League Cup",
      "slug": "league-slug",
      "url": null
    }
  }
];

input.forEach(x => {
  if (!output[x.league.id]) {
    output[x.league.id] = [];
  }

  output[x.league.id].push(x);
});

【讨论】:

    【解决方案2】:

    要对对象数组进行排序,您应该使用数组的 sort() 函数

    var yourArray = [{
        "id": 3401,
        "league": {
          "id": 4140,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3405,
        "league": {
          "id": 4145,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      },
      {
        "id": 3402,
        "league": {
          "id": 4143,
          "image_url": "pngurl.png",
          "modified_at": "2019-02-19T15:29:07Z",
          "name": "League Cup",
          "slug": "league-slug",
          "url": null
        }
      }
    ]
    
    yourArray.sort((a, b) => {
      if (a.league.id < b.league.id) {
        return -1;
      }
      if (a.league.id > b.league.id) {
        return 1;
      }
      return 0;
    })
    
    //Output will be sorted array
    console.log(yourArray);

    【讨论】:

      【解决方案3】:

      使用map 并在每个object.tournaments 上应用sort

      const sorted = (arr) =>
        arr.map((obj) => ({
          ...obj,
          tournaments: obj.tournaments.sort((a, b) => a.id - b.id),
        }));
        
      const data = [
        {
          league: "League Cup",
          tournaments: [
            {
              id: 3403,
              league: {
                id: 4141,
                image_url: "pngurl.png",
                modified_at: "2019-02-19T15:29:07Z",
                name: "League Cup",
                slug: "league-slug",
                url: null,
              },
            },
            {
              id: 3401,
              league: {
                id: 4141,
                image_url: "pngurl.png",
                modified_at: "2019-02-19T15:29:07Z",
                name: "League Cup",
                slug: "league-slug",
                url: null,
              },
            },
          ],
        },
        {
          league: "League Cup 2",
          tournaments: [
            {
              id: 3405,
              league: {
                id: 4145,
                image_url: "pngurl.png",
                modified_at: "2019-02-19T15:29:07Z",
                name: "League Cup 2",
                slug: "league-slug",
                url: null,
              },
            },
            {
              id: 3401,
              league: {
                id: 4145,
                image_url: "pngurl.png",
                modified_at: "2019-02-19T15:29:07Z",
                name: "League Cup 2",
                slug: "league-slug",
                url: null,
              },
            },
          ],
        },
      ];
      
      
      
      console.log(sorted(data));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多