mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Tweaks to Issue #53
This commit is contained in:
@@ -92,14 +92,13 @@
|
|||||||
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
// **Reduce** builds up a single result from a list of values, aka `inject`,
|
||||||
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
// or `foldl`. Delegates to **ECMAScript 5**'s native `reduce` if available.
|
||||||
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
_.reduce = _.foldl = _.inject = function(obj, iterator, memo, context) {
|
||||||
|
var initial = memo !== void 0;
|
||||||
if (nativeReduce && obj.reduce === nativeReduce) {
|
if (nativeReduce && obj.reduce === nativeReduce) {
|
||||||
if (context) iterator = _.bind(iterator, context);
|
if (context) iterator = _.bind(iterator, context);
|
||||||
var args = [iterator];
|
return initial ? obj.reduce(iterator, memo) : obj.reduce(iterator);
|
||||||
if (memo !== undefined) args.push(memo);
|
|
||||||
return obj.reduce.apply(obj, args);
|
|
||||||
}
|
}
|
||||||
each(obj, function(value, index, list) {
|
each(obj, function(value, index, list) {
|
||||||
if (memo === undefined && index == 0) {
|
if (!initial && index === 0) {
|
||||||
memo = value;
|
memo = value;
|
||||||
} else {
|
} else {
|
||||||
memo = iterator.call(context, memo, value, index, list);
|
memo = iterator.call(context, memo, value, index, list);
|
||||||
|
|||||||
Reference in New Issue
Block a user