【发布时间】: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