Last active
September 20, 2016 04:37
-
-
Save viebel/d5074a79db2bdb65f5e94627b901ba86 to your computer and use it in GitHub Desktop.
This file contains 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
// 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