我有一些包含关键“距离”的城市
ajax = new Location('123 main street', 'city, ON', 'L9Z 0K5', '905-555-5555', '905-555-555', 43.864362, -79.011627, 6);
alliston = new Location('117 Young Street', 'place, ON', 'L5R 0E9', '705-555-1234', '705-444-4321', 44.147691, -79.884193, 15);
aurora = new Location('2 New Place', 'capitol, ON', 'L8G 3W8', '905-999-0155', '905-727-5678', 44.009139, -79.470980, 1);
brampton = new Location('50 Circle Cres.', 'wendy, ON', 'L9r 8S1', '905-888-8888', null, 43.680537, -79.714164, 25);
这些对象(最后一个键是距离)存储在名为“城市”的数组中
如何遍历城市并打印距离最小的对象?
最佳答案
cities.reduce(function(a, b) {
return (b.distance < a.distance) ? b : a;
})
||
cities.sort(function(a, b) {
return a.distance - b.distance;
})[0]
关于javascript - 以最低值返回JavaScript数组中的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20365162/