Skip to content

Instantly share code, notes, and snippets.

@eatoncw
Last active February 16, 2018 03:09
Show Gist options
  • Save eatoncw/1e6616d8a85c96130ab7cee40fffb8cf to your computer and use it in GitHub Desktop.
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
//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