gpt4 book ai didi

javascript - 遍历数组并将结果过滤到 10

转载 作者:行者123 更新时间:2023-11-30 07:04:26 25 4
gpt4 key购买 nike

我正在使用 Google Maps Distance Matrix API搜索一个城市与许多其他城市之间的距离,返回一个如下所示的 array:

var array = [{
"distance": {
"text": "1,031 km",
"value": 1030580
},
"duration": {
"text": "9 hours 47 mins",
"value": 35216
},
"status": "OK",
"location": "Akron OH "
}, {
"distance": {
"text": "3,589 km",
"value": 3589092
},
"duration": {
"text": "1 day 8 hours",
"value": 116601
},
"status": "OK",
"location": "Albuquerque "
}, {
"distance": {
"text": "1,722 km",
"value": 1721588
},
"duration": {
"text": "16 hours 31 mins",
"value": 59462
},
"status": "OK",
"location": "Alpharetta "
}, {
"status": "ZERO_RESULTS",
"location": "Amsterdam "
}, {
"distance": {
"text": "1,465 km",
"value": 1465478
},
"duration": {
"text": "13 hours 47 mins",
"value": 49600
},
"status": "OK",
"location": "Asheville "
}, {
"distance": {
"text": "1,745 km",
"value": 1744913
},
"duration": {
"text": "16 hours 30 mins",
"value": 59408
},
"status": "OK",
"location": "Atlanta GA "
}, {
"distance": {
"text": "3,161 km",
"value": 3161495
},
"duration": {
"text": "1 day 5 hours",
"value": 103799
},
"status": "OK",
"location": "Austin TX "
}, {
"status": "ZERO_RESULTS",
"location": "Bali "
}, {
"distance": {
"text": "645 km",
"value": 645287
},
"duration": {
"text": "6 hours 31 mins",
"value": 23432
},
"status": "OK",
"location": "Baltimore "
}, {
"status": "ZERO_RESULTS",
"location": "Bangalore India "
}, {
"status": "ZERO_RESULTS",
"location": "Barcelona Spain "
}, {
"status": "ZERO_RESULTS",
"location": "Berlin "
}, {
"status": "ZERO_RESULTS",
"location": "Bogota Colombia "
}, {
"distance": {
"text": "1 m",
"value": 0
},
"duration": {
"text": "1 min",
"value": 0
},
"status": "OK",
"location": "Boston "
}, {
"distance": {
"text": "3,195 km",
"value": 3194920
},
"duration": {
"text": "1 day 5 hours",
"value": 103332
},
"status": "OK",
"location": "Boulder "
}, {
"status": "ZERO_RESULTS",
"location": "Brisbane "
}, {
"distance": {
"text": "347 km",
"value": 346944
},
"duration": {
"text": "3 hours 47 mins",
"value": 13627
},
"status": "OK",
"location": "Brooklyn NY "
}, {
"status": "ZERO_RESULTS",
"location": "Brussels "
}, {
"status": "ZERO_RESULTS",
"location": "Bucharest Romania "
}, {
"distance": {
"text": "4,161 km",
"value": 4160897
},
"duration": {
"text": "1 day 15 hours",
"value": 138992
},
"status": "OK",
"location": "Calgary "
}, {
"status": "ZERO_RESULTS",
"location": "Canaray Islands "
}, {
"distance": {
"text": "4,098 km",
"value": 4097538
},
"duration": {
"text": "1 day 13 hours",
"value": 131955
},
"status": "OK",
"location": "Cedar City "
}, {
"distance": {
"text": "1,572 km",
"value": 1571535
},
"duration": {
"text": "14 hours 46 mins",
"value": 53157
},
"status": "OK",
"location": "Charleston SC "
}, {
"distance": {
"text": "1,360 km",
"value": 1359772
},
"duration": {
"text": "13 hours 2 mins",
"value": 46920
},
"status": "OK",
"location": "Charlotte NC "
}, {
"distance": {
"text": "1,581 km",
"value": 1581073
},
"duration": {
"text": "14 hours 42 mins",
"value": 52919
},
"status": "OK",
"location": "Chicago IL "
}, {
"status": "ZERO_RESULTS",
"location": "Chile "
}, {
"distance": {
"text": "1,030 km",
"value": 1029774
},
"duration": {
"text": "9 hours 33 mins",
"value": 34376
},
"status": "OK",
"location": "Cleveland "
}, {
"status": "ZERO_RESULTS",
"location": "Cluj "
}, {
"status": "ZERO_RESULTS",
"location": "Columbia "
}, {
"distance": {
"text": "6,888 km",
"value": 6887507
},
"duration": {
"text": "3 days 8 hours",
"value": 286994
},
"status": "OK",
"location": "Costa Rica "
}, {
"status": "ZERO_RESULTS",
"location": "Crawley UK "
}, {
"distance": {
"text": "2,847 km",
"value": 2847374
},
"duration": {
"text": "1 day 2 hours",
"value": 93594
},
"status": "OK",
"location": "Dallas TX "
}, {
"distance": {
"text": "3,173 km",
"value": 3172885
},
"duration": {
"text": "1 day 5 hours",
"value": 102621
},
"status": "OK",
"location": "Denver CO "
}, {
"distance": {
"text": "1,138 km",
"value": 1137531
},
"duration": {
"text": "10 hours 46 mins",
"value": 38759
},
"status": "OK",
"location": "Detroit "
}, {
"status": "ZERO_RESULTS",
"location": "Durham England "
}, {
"status": "ZERO_RESULTS",
"location": "Ecuador "
}, {
"distance": {
"text": "3,137 km",
"value": 3137025
},
"duration": {
"text": "1 day 5 hours",
"value": 102899
},
"status": "OK",
"location": "Fort Collins "
}, {
"distance": {
"text": "2,378 km",
"value": 2377758
},
"duration": {
"text": "21 hours 35 mins",
"value": 77712
},
"status": "OK",
"location": "Fort Lauderdale "
}, {
"distance": {
"text": "1,515 km",
"value": 1514824
},
"duration": {
"text": "14 hours 24 mins",
"value": 51846
},
"status": "OK",
"location": "Greenville "
}, {
"status": "ZERO_RESULTS",
"location": "Hamptons "
}, {
"status": "ZERO_RESULTS",
"location": "Hongkong "
}, {
"status": "ZERO_RESULTS",
"location": "Honolulu HI "
}, {
"distance": {
"text": "2,974 km",
"value": 2973919
},
"duration": {
"text": "1 day 3 hours",
"value": 97180
},
"status": "OK",
"location": "Houston TX "
}, {
"status": "ZERO_RESULTS",
"location": "Hybrid "
}, {
"status": "ZERO_RESULTS",
"location": "Hyderabad India "
}]

我需要遍历此array 并返回一个array,其中包含十个距离最小的结果。您还会注意到一些结果显示 "status":"ZERO_RESULTS",所以我需要避免这些。

到目前为止,我的思路是这样的:

var arrayOfTen = [];
$.each(array,function(index,location){

})

最佳答案

这样做的方法是

  • 过滤掉不需要的条目
  • 数组按距离排序
  • 获取前10个元素

var result = array
.filter(function(city) {
return city.status === "OK";
})
.sort(function(a, b) {
return a.distance.value - b.distance.value;
})
.slice(0, 10);

或用arrow functions (感谢 ES2015):

var result = array
.filter(city => city.status === 'OK')
.sort((a, b) => a.distance.value - b.distance.value)
.slice(0, 10);

关于javascript - 遍历数组并将结果过滤到 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32790121/

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