Here you can find the source of reduce(combiner, initialValue)
// [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }); === [6]; // [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }, 10); === [16]; Array.prototype.reduce = function(combiner, initialValue) { var counter,/*from www .j a v a 2s. c o m*/ accValue; // If the array is empty, do nothing if (this.length === 0) { return this; } else { // If the user didn't pass an initial value, use the first item. if (arguments.length === 1) { counter = 1; accValue = this[0]; } else if (arguments.length >= 2) { counter = 0; accValue = initialValue; } else { throw "Invalid arguments."; } // Loop through the array, feeding the current value and the result of // the previous computation back into the combiner function until // we've exhausted the entire array and are left with only one value. while(counter < this.length) { accValue = combiner(accValue, this[counter]) counter++; } return [accValue]; } };
Array.prototype.reduce = function(callback, initial){ initial = initial || 0; for (var i=0; i< this.length; i++) { initial = callback(initial, this[i], i, this); return initial; wat = [1,2, 3, 4] console.log(wat); ...
Array.prototype.reduce = function(callback, initialValue) { var last, returned; this.forEach(function(item, index, array) { if (index == 0 && initialValue) { returned = initialValue; } else if (index == 1 && !initialValue) { returned = item; } else { last = returned, returned = null; ...
Array.prototype.reduce = function (callback, memo) { var obj = this; var value, accumulated_value = 0; for (var i = 0; i < obj.length; i++) { value = obj[i]; accumulated_value = callback.call(null, accumulated_value, value); return accumulated_value; }; ...
Array.prototype.reduce = function(cb, res){ var arr = Object(this); for(var i=0;i<arr.length;i++){ res = cb.call(res, arr[i], i); return res;
Array.prototype.reduce = function (combiner, initialValue) { var counter, accumulatedValue; if (this.length === 0) { return this; else { if (arguments.length === 1) { counter = 1; ...
Array.prototype.reduce = function(combiner, initialValue) { var counter, accumulatedValue; if (this.length === 0) { return this; else { if (arguments.length === 1) { counter = 1; ...
Array.prototype.reduce = function (combiner, initialValue) { var counter, accumulatedValue; if (this.length === 0) { return this; if (arguments.length === 1) { counter = 1; accumulatedValue = this[0]; ...
Array.prototype.reduce = function(combiner, initialValue) { var counter, accumulatedValue; if (this.length === 1) { return this; } else { if (arguments.length === 1) { counter = 1; accumulatedValue = this[0]; ...
Array.prototype.reduce = function(f, firstValue){ var value = firstValue; for (var i =0; i<this.length; i++){ value = f(this[i], value); return value; }; [1,2,3].reduce(function(currentValue ,value){return value+=currentValue;}, 0); [1,2,3].reduce(function(currentValue ,value){return value*=currentValue;}, 0); ...