gpt4 book ai didi

javascript - 根据数组中字符串值的索引对对象数组进行排序

转载 作者:行者123 更新时间:2023-11-28 17:26:30 25 4
gpt4 key购买 nike

我正在尝试根据字符串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
}
]

最佳答案

您需要在提供给 sort 的参数中同时使用 ab - 通过它们在 中索引的差异进行比较>[“所有者”、“管理员”、“主持人”] 数组。

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);

关于javascript - 根据数组中字符串值的索引对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51489053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com