Created
October 12, 2015 22:16
-
-
Save ivangeorgiev/51be65670642fc62f882 to your computer and use it in GitHub Desktop.
Array.max() Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the maximum value from array. | |
* @param getter Optional function for mapping array elements to actual comparison value. | |
* Examples: | |
* lengths.max(); | |
* persons.max(function(person) { return person.age; }); | |
*/ | |
Array.prototype.max = function(getter) { | |
var val; | |
getter = getter || function(v) { return v; } | |
return this.reduce(function(cur, el) { | |
val = getter(el); | |
return (typeof cur == "undefined") || val > cur ? val : cur; | |
}, undefined); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment