0

I have an array of cities that contain a key "distance"

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);

These objects (where the last key is distance) are stored in an array called cities

How can I loop through cities and print the object that contains the lowest distance?

0

4 Answers 4

2
cities.reduce(function(a, b) {
 return (b.distance < a.distance)  ? b : a;
})

||

cities.sort(function(a, b) {
 return a.distance - b.distance;
})[0]
Sign up to request clarification or add additional context in comments.

8 Comments

-1: The first one doesn't work; I tried it. It's using Array.reduce incorrectly.
@EthanBrown Care to explain? For what input does it fail?
Nope. I don't think you're getting how Array.reduce works. If you pass in Number.MIN_VALUE as a seed, the test will always fail (because Number.MIN_VALUE does not have a property distance, so you will always have (some number) < undefined, which will always return false). See my answer below using reduce, which works, and has been tested.
This is the array I've been testing against: [{distance:5},{distance:7},{distance:2}]. Your current incarnation, it returns 5e-324 (aka Number.MIN_VALUE). For your previous one, it returned {distance:5}.
@EthanBrown Thank you, you were patient with me and I appreciate it. You deserve an upvote. Please do tell if there's something wrong now?
|
1

You can use the reduce method of Array (I'm assuming here your Location object has a property called distance; adjust accordingly):

var nearest = [ajax,alliston,aurora,brampton]
    .reduce( function( nearest, location ) { 
        return !nearest || location.distance < nearest.distance 
            ? location : nearest;
    } );

Note: iccthedral's solution using reduce is a little neater than mine, thanks to his clever use of reduce's behavior if you don't supply an initial value. From MDN: "If no initialValue was provided, then [the first argument passed to the callback] will be equal to the first value in the array."

Comments

0

Put them into an object/array and create custom sort function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

...which will sort this array by distance.

Comments

0

I guess you want to use arrayObject.sort(sortby); and define your sortby function (e.g., return loc_a.distance - loc_b.distance).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.