Skip to content

Instantly share code, notes, and snippets.

@ivangeorgiev
Created October 12, 2015 22:16
Show Gist options
  • Save ivangeorgiev/51be65670642fc62f882 to your computer and use it in GitHub Desktop.
Save ivangeorgiev/51be65670642fc62f882 to your computer and use it in GitHub Desktop.
Array.max() Method
/**
* 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