Here you can find the source of reduce(fn, initialValue)
Array.prototype.reduce = function (fn, initialValue) { let acc = initialValue/*from w w w. j a v a 2 s . c om*/ for (var i = 0; i < this.length; i++) { acc = fn(acc, this[i], i) } return acc } Array.prototype.forEach = function (fn) { this.reduce((acc, curr, i) => { acc.push(fn(this[i], i)) return acc }, []) } Array.prototype.filter = function (fn) { return this.reduce((acc, curr, i) => { if (fn(this[i])) { acc.push(this[i]) } return acc }, []) } Array.prototype.map = function (fn) { return this.reduce((acc, curr, i) => { acc.push(fn(this[i])) return acc }, []) } Array.prototype.concatAll = function () { return this.reduce((acc, curr, i) => { curr.forEach((item) => acc.push(item)) return acc }, []) } Array.prototype.concatMap = function (fnReturningArray) { return this.map((item) => fnReturningArray(item)) .concatAll() } Array.zip = function (left, right, combinerFn) { 'use strict' let counter let result = [] for (let i = 0; i < Math.min(left.length, right.length); i++) { result.push(combinerFn(left[i], right[i])) } return result }
Array.prototype.reduce = function (f, value) { for (var i = 0, len = this.length; i < len; i++) { value = f(this[i], value); return value; }; var superheroes = ['superman', 'iron man', 'spiderman', 'batman']; var totalLength = superheroes.reduce(function (elem, acc) { return elem.length + acc; ...
var data = [ 4, 8, 15, 16, 23, 42 ]; Array.prototype.reduce = function(f, value) { var i; for (i = 0; i < this.length; i += 1) { value = f(this[i], value); return value; }; var add = function(a, b) { ...
var is_array_01 = function (value) { return value && typeof value === 'object' && value.constructor === Array; }; var is_array = function (value) { return value && typeof value === 'object' && typeof value.length === 'number' && ...
Array.prototype.reduce = function(f, value) { var i; for (i = 0; i < this.length; i += 1) { value = f(this[i], value); return value; }; var data = [ 4, 8, 15, 16, 23, 42 ]; var add = function(a, b) { ...
Array.prototype.reduce = function(fn, initial) var r, i; initial ? r = initial : r = this[0]; for (i=1;i<this.length;i++) r = fn(r, this[i]); return r; ...
if (!Array.prototype.reduce) { Array.prototype.reduce = function(fun ) { var len = this.length >>> 0; if (typeof fun != "function") throw new TypeError(); if (len == 0 && arguments.length == 1) throw new TypeError(); var i = 0; if (arguments.length >= 2) { ...
Array.prototype.reduce = function(fun ) var len = this.length; if (typeof fun != "function") { throw new TypeError(); if (len == 0 && arguments.length == 1) { throw new TypeError(); var i = 0; if (arguments.length >= 2) { var rv = arguments[1]; } else { do { if (i in this) { rv = this[i++]; break; if (++i >= len) { throw new TypeError(); } while (true); for (; i < len; i++) { if (i in this) { rv = fun.call(null, rv, this[i], i, this); return rv; };
Array.prototype.reduce = Array.prototype.reduce || function(fun ) { var len = this.length >>> 0; if (typeof fun != "function") { throw new TypeError(); if (len == 0 && arguments.length == 1) { throw new TypeError(); var i = 0; ...
'use strict'; Array.prototype.reduce = function(pasteback, initial){ for(var i = 0 , c = this.length , value = arguments.length>1 ? initial : this[i++] ; i<c; i++){ value = pasteback(value, this[i], i, this); return value; ...