0

Ok take the following: fiddle

how do i run a for each collecting the highest value for each of the north values and the lowest for the south in javascript?

Ok i went for this:

    function finder(cmp, arr, attr) {
       var val = arr[0][attr];
       for (var i = 1; i < arr.length; i++) {
           val = cmp(val, arr[i][attr]);
       }
       return val;
   }
6
  • Give it a shot! PS: do you realize that what you're trying to do is not a reliable way to get the bounding box? Commented May 19, 2014 at 10:53
  • from that answer I'm guessing there isn't a defacto way to do it! Commented May 19, 2014 at 10:54
  • There are nice ways of doing that with, say, Math.min/Math.max Commented May 19, 2014 at 10:54
  • what algorithm have you tried so far? Commented May 19, 2014 at 10:55
  • 1
    Please note that you only have Strings, you should parse the numbers out of them too Commented May 19, 2014 at 11:09

1 Answer 1

2

Angular has a lovely built in forEach function:

var lowestFound = false;
var lowest = 0;

angular.forEach(values, function(value, key) {
  if (value < lowest || lowestFound === false) {
    lowest = value;
    lowestFound = true;
  }
});

return lowest;

Not perfect, but it should work. :)

Sign up to request clarification or add additional context in comments.

2 Comments

uuu thats a nice way of doing it!
You could also add similar code for the highest values in the same function (saving looping over everything twice). :)

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.