当我从 https://maps.googleapis.com/maps/api/distancematrix/json?origins=10010&destinations=10011|10012|10013&key=AIzaSyBOsjeskUwOLYQZntsv7_34gVZ3xgcXko0获取数据时
它返回:
{
"destination_addresses" : [
"New York, NY 10011, USA",
"New York, NY 10012, USA",
"New York, NY 10013, USA"
],
"origin_addresses" : [ "New York, NY 10010, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "3.4 km",
"value" : 3389
},
"duration" : {
"text" : "19 mins",
"value" : 1156
},
"status" : "OK"
},
{
"distance" : {
"text" : "2.6 km",
"value" : 2632
},
"duration" : {
"text" : "13 mins",
"value" : 767
},
"status" : "OK"
},
{
"distance" : {
"text" : "9.5 km",
"value" : 9545
},
"duration" : {
"text" : "19 mins",
"value" : 1143
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
但是我需要按距离排序的数据。先显示小后大。
可能吗?
当然! 1. 将 JSON 数据解析为对象 (JSON.parse(; 2. 使用代码对元素数组进行排序,如下所示:
// given els is one of the elements extracted from this object:
let sorted = els.sort((a, b) => a.distance.value - b.distance.value);