Fix reduceRight() so that if you don't pass in an initial value, the last item in the collection is used

This commit is contained in:
Elliot Winkler
2010-10-31 19:00:21 -06:00
parent 31efd854f8
commit f729769d85
2 changed files with 11 additions and 3 deletions

View File

@@ -107,7 +107,9 @@
_.reduceRight = _.foldr = function(obj, iterator, memo, context) {
if (nativeReduceRight && obj.reduceRight === nativeReduceRight) {
if (context) iterator = _.bind(iterator, context);
return obj.reduceRight(iterator, memo);
var args = [iterator];
if (memo !== undefined) args.push(memo);
return obj.reduceRight.apply(obj, args);
}
var reversed = (_.isArray(obj) ? obj.slice() : _.toArray(obj)).reverse();
return _.reduce(reversed, iterator, memo, context);