Skip to content

Instantly share code, notes, and snippets.

@viebel viebel/maxBy_minBy.js
Last active Sep 20, 2016

What would you like to do?
// Thank you
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
You can’t perform that action at this time.