javaScript 对json数据按key值排序

时间:2022-06-23 21:19:08
 var ajson=
{
"result":[
{
"cid":1,
"name":"aaa",
"price":1000
},{
"cid":2,
"name":"bbb",
"price":150
},{
"cid":3,
"name":"ccc",
"price":200
},{
"cid":4,
"name":"ddd",
"price":1500
},{
"cid":5,
"name":"eee",
"price":1100
}
],
"totalCount":5
} function sortJ(a,b){
return a.price-b.price;
};
var data=ajson.result.sort(sortJ); console.log(data); 控制台输出结果
 [Object, Object, Object, Object, Object]
0
:
Object
cid
:
2
name
:
"bbb"
price
:
150
__proto__
:
Object
1
:
Object
cid
:
3
name
:
"ccc"
price
:
200
__proto__
:
Object
2
:
Object
cid
:
1
name
:
"aaa"
price
:
1000
__proto__
:
Object
3
:
Object
cid
:
5
name
:
"eee"
price
:
1100
__proto__
:
Object
4
:
Object
cid
:
4
name
:
"ddd"
price
:
1500
__proto__
:
Object
length
:
5
__proto__
:
Array[0]