好吧所以我试图对Javascript对象或Javascript数组对象中的一些结果进行排序,但我似乎无法破解这一个,但我希望能够按不同的键排序值..
我尝试了什么(使用“金额”键)
(想象一下,可排序变量包含Javascript对象或Javascript对象数组)
var sortable;
sortable.sort(function (a, b) {
return a.amount > b.amount;
});
console.log(sortable);
Javascript对象
"1": {
"BTC": {
"name": "Bitcoin",
"symbol": "BTC",
"amount": "5.0000000000",
"rank": "1",
"usd_coin": "3580.68",
"btc_coin": "1.00",
"volume": "1324380000.00",
"marketcap": "59337865239.00",
"1h": "1.62",
"24h": "0.65",
"7d": "-12.23"
},
"ETH": {
"name": "Ethereum",
"symbol": "ETH",
"amount": "1153.0000000000",
"rank": "2",
"usd_coin": "247.05",
"btc_coin": "0.07",
"volume": "421132000.00",
"marketcap": "23388713395.00",
"1h": "2.06",
"24h": "1.70",
"7d": "-12.80"
},
"LTC": {
"name": "Litecoin",
"symbol": "LTC",
"amount": "15.0000000000",
"rank": "5",
"usd_coin": "48.84",
"btc_coin": "0.01",
"volume": "276411000.00",
"marketcap": "2587009816.00",
"1h": "2.90",
"24h": "1.84",
"7d": "-20.80"
}
}
对象数组
[
[
{
"name": "Bitcoin",
"symbol": "BTC",
"amount": "5.0000000000",
"rank": "1",
"usd_coin": "3580.68",
"btc_coin": "1.00",
"volume": "1324380000.00",
"marketcap": "59337865239.00",
"1h": "1.62",
"24h": "0.65",
"7d": "-12.23"
}
],
[
{
"name": "Ethereum",
"symbol": "ETH",
"amount": "1153.0000000000",
"rank": "2",
"usd_coin": "247.05",
"btc_coin": "0.07",
"volume": "421132000.00",
"marketcap": "23388713395.00",
"1h": "2.06",
"24h": "1.70",
"7d": "-12.80"
}
],
[
{
"name": "Litecoin",
"symbol": "LTC",
"amount": "15.0000000000",
"rank": "5",
"usd_coin": "48.84",
"btc_coin": "0.01",
"volume": "276411000.00",
"marketcap": "2587009816.00",
"1h": "2.90",
"24h": "1.84",
"7d": "-20.80"
}
]
]
答案 0 :(得分:1)
有关排序数组的信息,请参阅MDN: Array.prototype.sort()上的示例。 sort函数允许您将自己的自定义排序功能传递给它。
也可以使用地图功能。