Skip to content

Instantly share code, notes, and snippets.

@Fintan
Created January 16, 2014 16:08
Show Gist options
  • Save Fintan/8457598 to your computer and use it in GitHub Desktop.
Save Fintan/8457598 to your computer and use it in GitHub Desktop.
Using reduce to find the smallest/largest number reduce_.reduce(list, iterator, memo, [context]) Aliases: inject, foldl Also known as inject and foldl, reduce boils down a list of values into a single value. Memo is the initial state of the reduction, and each successive step of it should be returned by iterator. The iterator is passed four argu…
var smallest = _.reduce([4, 44, 3, 6, 2], function(memo, num){
return memo > num ? num : memo;
});
//2
var largest = _.reduce([4, 44, 3, 6, 2], function(memo, num){
return memo < num ? num : memo;
});
//44
var smallestObject = _.reduce(objectArray, function(memo, obj){
return memo > obj.value ? obj.value : memo;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment