【发布时间】:2018-07-05 17:13:46
【问题描述】:
在您将其标记为重复之前 - 我已经阅读了这些答案:
Sort a JSON array object using Javascript by value
我已经尝试将代码移到我的数据中,但它不起作用。我也尝试将每个对象项推入一个数组,但这不起作用,因为每个项内部仍然有一个对象。
Here's a bit of the JSON response I get(浏览器控制台截图)
如您所见,每个项目都有一个索引 - 我需要的是根据索引对响应进行排序。我该怎么做呢?
此回复来自维基百科 - 顺便说一句。 Here's the full API call,如果有帮助的话。
编辑:在此处发布一些响应代码:
"pages": {
"736": {
"pageid": 736,
"ns": 0,
"title": "Albert Einstein",
"index": 2,
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2018-01-24T22:40:11Z",
"lastrevid": 821432412,
"length": 145560,
"fullurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
"editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Einstein&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Albert_Einstein",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/38px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg",
"width": 38,
"height": 50
},
"pageimage": "Einstein_1921_by_F_Schmutzer_-_restoration.jpg"
},
"983": {
"pageid": 983,
"ns": 0,
"title": "Albert Camus",
"index": 10,
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2018-01-26T09:34:35Z",
"lastrevid": 822358239,
"length": 53639,
"fullurl": "https://en.wikipedia.org/wiki/Albert_Camus",
"editurl": "https://en.wikipedia.org/w/index.php?title=Albert_Camus&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Albert_Camus",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/0/08/Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg/42px-Albert_Camus%2C_gagnant_de_prix_Nobel%2C_portrait_en_buste%2C_pos%C3%A9_au_bureau%2C_faisant_face_%C3%A0_gauche%2C_cigarette_de_tabagisme.jpg",
"width": 42,
"height": 50
},
"pageimage": "Albert_Camus,_gagnant_de_prix_Nobel,_portrait_en_buste,_posé_au_bureau,_faisant_face_à_gauche,_cigarette_de_tabagisme.jpg"
},
"46721": {
"pageid": 46721,
"ns": 0,
"title": "Edward VII",
"index": 9,
"contentmodel": "wikitext",
"pagelanguage": "en",
"pagelanguagehtmlcode": "en",
"pagelanguagedir": "ltr",
"touched": "2018-01-26T02:00:27Z",
"lastrevid": 821663314,
"length": 81925,
"fullurl": "https://en.wikipedia.org/wiki/Edward_VII",
"editurl": "https://en.wikipedia.org/w/index.php?title=Edward_VII&action=edit",
"canonicalurl": "https://en.wikipedia.org/wiki/Edward_VII",
"thumbnail": {
"source": "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Edward_VII_in_coronation_robes.jpg/35px-Edward_VII_in_coronation_robes.jpg",
"width": 35,
"height": 50
},
"pageimage": "Edward_VII_in_coronation_robes.jpg"
}
【问题讨论】:
-
您能以代码形式而不是图像发布您尝试排序的 JSON 对象吗?
-
如果您想做一些如此具体的事情,以至于这里没有其他问题可以解决您的问题,您不应该告诉我们您想做什么以及为什么其他技术不适用吗?
-
你想按什么排序哪个数组?
-
@devius 响应非常大,这就是为什么我没有在正文中发布 - 但我现在已经做到了:) @NinaScholz
index。不幸的是,它不是一个数组,它是我得到的 JSON 响应,我想要一种方法来按对象中每个项目所具有的index对这个响应进行排序。 -
那么,为什么你把这个标题放在 按键值排序 JSON 响应
标签: javascript arrays json sorting object