Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
// Thank you http://stackoverflow.com/a/32234086/813665
var extremumBy = function(pluck, extremum, arr) {
return arr.reduce(function(best, next) {
var pair = [pluck(next), next];
if (best === null) {
return pair;
}
if (extremum(best[0], pair[0]) === best[0]) {
return best;
}
return pair;
}, null)[1];
};
minBy = function(fn, arr) {
return extremumBy(fn, Math.min, arr);
};
maxBy = function(fn, arr) {
return extremumBy(fn, Math.max, arr);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment