Javascript 根据字符串值对对象数组进行排序';数组中的索引
我正在尝试根据字符串Javascript 根据字符串值对对象数组进行排序';数组中的索引,javascript,arrays,Javascript,Arrays,我正在尝试根据字符串rank在数组中的位置对数据集进行排序。我尝试将indexOf与sort结合使用,但效果有限。我确实发现了另一篇类似的帖子,尽管它与我对任何快速复制记者的问题无关 数据集: [ { "_id": { "$oid": "5b535f6eddfad00564b103db" }, "notes": [ "Very attractive" ], "warns": [], "username": "
rank
在数组中的位置对数据集进行排序。我尝试将indexOf与sort结合使用,但效果有限。我确实发现了另一篇类似的帖子,尽管它与我对任何快速复制记者的问题无关
数据集:
[
{
"_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
}
]
您需要在提供给排序的参数中同时使用
a
和b
——通过[“所有者”、“管理员”、“版主”]
数组中它们的标记的差异进行比较
const-input=[{“\u-id”:{“$oid”:“5b535f6eddfad00564b103db”},注释:[“非常吸引人”],“警告”:[],“用户名”:“Saddy”,“化身”:https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/3c/3ce7fd0c1a5225790d67d90d4f1e60d2d29d2037_full.jpg,“id:“76561198151478478”,“dateHired:{“$date:“2018-07-21T16:29:34.563Z”},“简介”:"https://steamcommunity.com/id/dpitt/,“颁布禁令”:0,“当前票证”:0,“总票证”:0,“等级”:“管理员”,“u v”:0},{“u id”:{“$oid”:“5b54d085ed4855275f4cea63”},注释:[],“警告”:[],“用户名”:“meme master nick”,“化身”: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/,“颁布的禁令”:0,“当前票证”:0,“总票证”:0,“等级”:“所有者”,“u v”:0},{“u id”:{“$oid”:“5b567169e7179a32d9cc2abe”},注释:[],“警告”:[],“用户名”:“DarkGamer2k16”,“化身”:"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/“,“颁布禁令”:0,“当前票证”:0,“总票证”:0,“排名”:“主持人”,“u v”:0}]
const排名=[“所有者”、“管理员”、“版主”];
输入.sort((a,b)=>ranks.indexOf(a.rank)-ranks.indexOf(b.rank));
console.log(输入);
您需要在提供给排序的参数中同时使用a
和b
——通过[“所有者”、“管理员”、“版主”]
数组中的标记差异进行比较
const-input=[{“\u-id”:{“$oid”:“5b535f6eddfad00564b103db”},注释:[“非常吸引人”],“警告”:[],“用户名”:“Saddy”,“化身”: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/,“颁布禁令”:0,“当前票证”:0,“总票证”:0,“等级”:“管理员”,“u v”:0},{“u id”:{“$oid”:“5b54d085ed4855275f4cea63”},注释:[],“警告”:[],“用户名”:“meme master nick”,“化身”: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/,“颁布的禁令”:0,“当前票证”:0,“总票证”:0,“等级”:“所有者”,“u v”:0},{“u id”:{“$oid”:“5b567169e7179a32d9cc2abe”},注释:[],“警告”:[],“用户名”:“DarkGamer2k16”,“化身”:"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/“,“颁布禁令”:0,“当前票证”:0,“总票证”:0,“排名”:“主持人”,“u v”:0}]
const排名=[“所有者”、“管理员”、“版主”];
输入.sort((a,b)=>ranks.indexOf(a.rank)-ranks.indexOf(b.rank));
console.log(输入)
预期的输出是什么样子的?我会在帖子中更新它,一秒钟。预期的输出是什么样子的?我会在帖子中更新它,一秒钟。谢谢!我已经坚持了一段时间了。一旦可以,我会将你的标记为正确。谢谢!我已经坚持了一段时间了。一旦可以,我会将你的标记为正确。