gpt4 book ai didi

javascript - 如何按键对字典数组进行排序?

转载 作者:行者123 更新时间:2023-12-01 03:54:40 24 4
gpt4 key购买 nike

var arr = [ 
{ 'name': 'ashley', 'score': 5 },
{ 'name': 'jason', 'score': 9 },
{ 'name': 'ben', 'score': 1 },
{ 'name': 'jordan', 'score': 4 },
]

如何重新排序该数组以使结果按分数降序排列?

最佳答案

你可以这样做:

arr.sort(function(a,b) {
return b.score - a.score
});

这给出:

[
{"name":"jason","score":9},
{"name":"ashley","score":5},
{"name":"jordan","score":4},
{"name":"ben","score":1}
]

关于javascript - 如何按键对字典数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42870356/

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