迭代数组并将结果过滤到10

时间:2015-09-25 20:24:56

标签: javascript jquery object

我正在使用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,其中包含10个距离最小的结果。你还会注意到一些结果说"status":"ZERO_RESULTS",所以我需要避免这些。

到目前为止,我正在考虑这些问题:

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

})

2 个答案:

答案 0 :(得分:10)

这样做的方法是

  • 过滤掉不需要的条目
  • 按距离排序数组
  • 获取前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);

答案 1 :(得分:1)

如果要在数组中找到n个最小的条目,只要数组很小,就可以对整个事物进行排序。但是如果你有一个大数组和一个小数组,那么就有更快的方法。

function findNSmallest(array, n, comp) {
  var result = [];

  for(var i = 0; i < array.length; i++) {
    var elem = array[i];
    for(var j = result.length - 1; j >= 0 && comp(elem, result[j]) < 0; j--) {
      if(j + 1 < n) result[j + 1] = result[j];
    }
    if(j + 1 < n) result[j + 1] = elem;
  }
  return result;
}

顺便说一句。 comp是比较函数。