Created
January 16, 2014 16:08
-
-
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…
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
| 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