我正在尝试根据字符串rank
在数据集中的位置对它们进行排序。我尝试将indexOf与排序一起使用只能获得有限的成功。我确实找到了另一篇类似的文章,尽管它与我的问题无关(对于任何重复的记者)。
数据集:
[
{
"_id": {
"$oid": "5b535f6eddfad00564b103db"
},
"notes": [
"Very attractive"
],
"warns": [],
"username": "Saddy",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg",
"id": "76561198151478478",
"dateHired": {
"$date": "2018-07-21T16:29:34.563Z"
},
"profile": "https://steamcommunity.com/id/dpitt/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Administrator",
"__v": 0
},
{
"_id": {
"$oid": "5b54d085ed4855275f4cea63"
},
"notes": [],
"warns": [],
"username": "meme master nick",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg",
"id": "76561198353773365",
"dateHired": {
"$date": "2018-07-22T18:44:21.814Z"
},
"profile": "https://steamcommunity.com/profiles/76561198353773365/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Owner",
"__v": 0
},
{
"_id": {
"$oid": "5b567169e7179a32d9cc2abe"
},
"notes": [],
"warns": [],
"username": "DarkGamer2k16",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg",
"id": "76561194018842253",
"dateHired": {
"$date": "2018-07-21T16:29:34.563Z"
},
"profile": "https://steamcommunity.com/id/DarkGamer2k16/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Moderator",
"__v": 0
}
]
尝试失败:
console.log(arr.sort(function(a){
return ["Owner", "Administrator", "Moderator"].indexOf(a.rank)
}))
arr.sort(function(a,b){
let sort = Object.assign({}, a, b);
return["Owner", "Administrator", "Moderator"].indexOf(sort.rank)
})
上面的都只是用我的替换所有数据。谢谢您的协助
预期输出:
[
{
"_id": {
"$oid": "5b54d085ed4855275f4cea63"
},
"notes": [],
"warns": [],
"username": "meme master nick",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg",
"id": "76561198353773365",
"dateHired": {
"$date": "2018-07-22T18:44:21.814Z"
},
"profile": "https://steamcommunity.com/profiles/76561198353773365/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Owner",
"__v": 0
},
{
"_id": {
"$oid": "5b535f6eddfad00564b103db"
},
"notes": [
"Very attractive"
],
"warns": [],
"username": "Saddy",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg",
"id": "76561198151478478",
"dateHired": {
"$date": "2018-07-21T16:29:34.563Z"
},
"profile": "https://steamcommunity.com/id/dpitt/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Administrator",
"__v": 0
},
{
"_id": {
"$oid": "5b567169e7179a32d9cc2abe"
},
"notes": [],
"warns": [],
"username": "DarkGamer2k16",
"avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg",
"id": "76561194018842253",
"dateHired": {
"$date": "2018-07-21T16:29:34.563Z"
},
"profile": "https://steamcommunity.com/id/DarkGamer2k16/",
"enactedBans": 0,
"currentTickets": 0,
"totalTickets": 0,
"rank": "Moderator",
"__v": 0
}
]
答案 0 :(得分:3)
您需要在提供给a
的参数中同时使用b
和sort
-通过["Owner", "Administrator", "Moderator"]
数组中它们的索引差异进行比较。
const input=[{"_id":{"$oid":"5b535f6eddfad00564b103db"},"notes":["Very attractive"],"warns":[],"username":"Saddy","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg","id":"76561198151478478","dateHired":{"$date":"2018-07-21T16:29:34.563Z"},"profile":"https://steamcommunity.com/id/dpitt/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Administrator","__v":0},{"_id":{"$oid":"5b54d085ed4855275f4cea63"},"notes":[],"warns":[],"username":"meme master nick","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f6/f61f5d62b79b22c9183c049521a4c587ab9334cd_full.jpg","id":"76561198353773365","dateHired":{"$date":"2018-07-22T18:44:21.814Z"},"profile":"https://steamcommunity.com/profiles/76561198353773365/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Owner","__v":0},{"_id":{"$oid":"5b567169e7179a32d9cc2abe"},"notes":[],"warns":[],"username":"DarkGamer2k16","avatar":"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3d/3dd1f81fc4556ae984ebe2312e1b9fc4ba54b5ea_full.jpg","id":"76561194018842253","dateHired":{"$date":"2018-07-21T16:29:34.563Z"},"profile":"https://steamcommunity.com/id/DarkGamer2k16/","enactedBans":0,"currentTickets":0,"totalTickets":0,"rank":"Moderator","__v":0}]
const ranks = ["Owner", "Administrator", "Moderator"];
input.sort((a, b) => ranks.indexOf(a.rank) - ranks.indexOf(b.rank));
console.log(input);