Created
October 17, 2011 15:20
-
-
Save jsmecham/1292846 to your computer and use it in GitHub Desktop.
Useful Underscore.js Extensions
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
(function() { | |
// | |
// Iterates over an array of numbers and returns the sum. Example: | |
// | |
// _.sum([1, 2, 3]) => 6 | |
// | |
_.sum = function(obj) { | |
if (!$.isArray(obj) || obj.length == 0) return 0; | |
return _.reduce(obj, function(sum, n) { | |
return sum += n; | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With mixin:
.... allows for _([1, 2, 3]).sum() => 6