Skip to content

Instantly share code, notes, and snippets.

@ivangeorgiev
Created October 12, 2015 22:15
Show Gist options
  • Save ivangeorgiev/dccdbfa99beb0f940a77 to your computer and use it in GitHub Desktop.
Save ivangeorgiev/dccdbfa99beb0f940a77 to your computer and use it in GitHub Desktop.
Array.min() Method
/**
* Get the minimum value from array.
* @param getter Optional function for mapping array elements to actual comparison value.
* Examples:
* lengths.min();
* persons.min(function(person) { return person.age; });
*/
Array.prototype.min = 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