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*

11
  • 18
    This is a good answer, however, you would like to pass an initial value or you would get an error in case the data array is empty. i.e. for an autoincrement index of objects. const max = data.reduce((prev, current) => (prev.y > current.y) ? prev : current, 1) Commented May 25, 2017 at 15:56
  • 7
    You raise a good point, I would probably choose null over 1. Commented May 30, 2017 at 10:26
  • 72
    Note that this returns the object that had the max value not the max value from the object. This may or may not be what you want. In my case it was what I wanted. +1 Commented Nov 24, 2017 at 23:46
  • Excellent answer! At first, I hesitated due to the reduce, but we're gonna need to iterate anyway, so why not? Commented Feb 26, 2019 at 10:09
  • 2
    Also, make sure the data type is a number and not a string. Otherwise you could get mixed results. Use parseInt() where appropriate. Commented Jan 6, 2022 at 0:55