mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Merge branch 'fix_reduce_initial_value' of http://github.com/mcmire/underscore
This commit is contained in:
@@ -65,6 +65,9 @@ $(document).ready(function() {
|
||||
|
||||
sum = _([1, 2, 3]).reduce(function(sum, num){ return sum + num; }, 0);
|
||||
equals(sum, 6, 'OO-style reduce');
|
||||
|
||||
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
|
||||
equals(sum, 6, 'default initial value');
|
||||
});
|
||||
|
||||
test('collections: reduceRight', function() {
|
||||
|
||||
@@ -94,10 +94,16 @@
|
||||
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
||||
if (nativeReduce && obj.reduce === nativeReduce) {
|
||||
if (context) iterator = _.bind(iterator, context);
|
||||
return obj.reduce(iterator, memo);
|
||||
var args = [iterator];
|
||||
if (memo !== undefined) args.push(memo);
|
||||
return obj.reduce.apply(obj, args);
|
||||
}
|
||||
each(obj, function(value, index, list) {
|
||||
memo = iterator.call(context, memo, value, index, list);
|
||||
if (memo === undefined && index == 0) {
|
||||
memo = value;
|
||||
} else {
|
||||
memo = iterator.call(context, memo, value, index, list);
|
||||
}
|
||||
});
|
||||
return memo;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user