Nodejs Array Reduce reduce(fun /*, initial*/)

Here you can find the source of reduce(fun /*, initial*/)

Method Source Code

/**/*from  w w w  . j  ava2  s  . com*/
 * Applies `fun` to `initial` and the first element of the array, and then to the
 * result and the second element, and so on.  Returns the result of applying
 * `fun` to the accumulated value and the last element of the array.
 *
 * `fun` is given four arguments:
 * <ol>
 * <li>the result of the previous invocation of `fun`, or the initial value on the first invocation</li>
 * <li>a single array element</li>
 * <li>the index of that element in the array</li>
 * <li>the original array</li>
 * </ol>
 *
 * The 'reduce' algorithm is also known as 'fold' and 'inject'.
 *
 * This definition is *not* compatible with the definitions of `Array#reduce`
 * or `Array#inject` in the Prototype library.  However it is compatible with
 * the JavaScript 1.6 definition of `Array#reduce` in Spidermonkey.
 *
 * This implementation comes from:
 * https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce
 *
 * @function
 * @param   {Function}  fun     function that will be applied to each array element
 * @param   {any}       [initial]   initial value; defaults to the first array element
 * @returns {any}       the return value from the last invocation of `fun`
 */
Array.prototype.reduce = Array.prototype.reduce || function(fun /*, initial*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function") {
        throw new TypeError();
    }

    // no value to return if no initial value and an empty array
    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 array contains no values, no initial value to return
            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;
};

Related

  1. reduce(f, value)
    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) {
    ...
    
  2. reduce(fn, initial)
    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;
    ...
    
  3. reduce(fn, initialValue)
    Array.prototype.reduce = function (fn, initialValue) {
      let acc = initialValue
      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) => {
    ...
    
  4. reduce(fun /* , initial */)
    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) {
    ...
    
  5. reduce(fun /*, initial*/)
    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;
    };
    
  6. reduce(pasteback, initial)
    '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;
    ...
    
  7. reduce(process, initial)
    Array.prototype.reduce = function(process, initial) {
      var value = initial;
      for (var i = 0; i < this.length; i++) {
        if( ( i === 0 ) && ( value === undefined || value === null ))
            value = this[0];
            continue;
        value = process( value, this[i] );
    ...
    
  8. reduce(process, initial)
    Array.prototype.reduce = function(process, initial) {
      if (initial === undefined) {
        var slice = this.slice(1);
        initial = this[0];
      var array = slice || this;
      array.forEach(function(value) {
        initial = process(initial, value);
      });
    ...
    
  9. reduce(reducingFunction, initialValue)
    Array.prototype.reduce = function(reducingFunction, initialValue) {
      var result = initialValue;
      this.forEach(function(x) {
        reducingFunction(result, x)
      });
      return result;