Last active
February 16, 2018 03:09
-
-
Save eatoncw/1e6616d8a85c96130ab7cee40fffb8cf to your computer and use it in GitHub Desktop.
Javascript find max value in array of objects with key as argument, using reduce
This file contains hidden or 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
//find max value in array of objects with key as argument | |
function findMax(key) { | |
return function maxx(prev,curr) { | |
return Math.max(prev, curr[key]); | |
} | |
} | |
//then pass function to reduce with key as argument | |
//eg get max price from an array of objects | |
var maxPrice = array.reduce(findMax("price"),0); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment