gpt4 book ai didi

javascript - 根据属性值对 JSON 数组进行排序

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:11 25 4
gpt4 key购买 nike

这是我得到的 JSON 数组:

    [
{
"Name" : "Sachin",
"Age" : "41",
"Team" : "Mumbai"
},
{
"Name" : "Dravid",
"Age" : "42",
"Team" : "Rajasthan"
},
{
"Name" : "Yuvraj",
"Age" : "31",
"Team" : "Bangalore"
}
]

但我需要按“年龄”属性对这个 JSON 数组 desc 进行排序。我想要的 JSON 数组应该如下所示:

 [
{
"Name" : "Dravid",
"Age" : "42",
"Team" : "Rajasthan"
},
{
"Name" : "Sachin",
"Age" : "41",
"Team" : "Mumbai"
},
{
"Name" : "Yuvraj",
"Age" : "31",
"Team" : "Bangalore"
}
]

如何实现?

最佳答案

var a = [
{
"Name" : "Sachin",
"Age" : "41",
"Team" : "Mumbai"
},
{
"Name" : "Dravid",
"Age" : "42",
"Team" : "Rajasthan"
},
{
"Name" : "Yuvraj",
"Age" : "31",
"Team" : "Bangalore"
}
];

a.sort(function(x,y){return y["Age"]-x["Age"]});
console.log(a);

关于javascript - 根据属性值对 JSON 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24503187/

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