Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • This makes sense, I've been stuck with thinking about comparing data inside the array to each other instead of an external high/low number. Commented Jan 14, 2012 at 19:01
  • 2
    The only thing I would change is setting lowest and highest is a bit redundant. I would rather loop one less time and set lowest=highest=myArray[0] and then start the loop at 1. Commented Jan 14, 2012 at 19:13
  • 1
    @32bitkid Good point. should be myArray[0].Cost, though. But, if there's no first element, an error will be thrown. So, an additional check is needed, possibly undoing the small performance boost. Commented Jan 14, 2012 at 19:17
  • 1
    @Wilt Yes, maintain another variable that gets updated when you've found a lowest value, i.e. var lowestObject; for (...) and if (tmp < lowest) { lowestObject = myArray[i]; lowest = tmp; } Commented Mar 11, 2014 at 8:58
  • 3
    This answer is very old, before ECMAScript 2015 (ES6) went out. Was right at the time, but now that answer is a better option. Commented Sep 26, 2020 at 19:06