Nodejs Array Reduce reduce(combiner, initialValue)

Here you can find the source of reduce(combiner, initialValue)

Method Source Code

// [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];
   }
};

Related

  1. reduce(callback, initial)
    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);
    ...
    
  2. reduce(callback, initialValue)
    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;
    ...
    
  3. reduce(callback, memo)
    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;
    };
    ...
    
  4. reduce(cb, res)
    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;
    
  5. reduce(combiner, initialValue)
    Array.prototype.reduce = function (combiner, initialValue) {
      var counter,
        accumulatedValue;
      if (this.length === 0) {
        return this;
      else {
        if (arguments.length === 1) {
          counter = 1;
    ...
    
  6. reduce(combiner, initialValue)
    Array.prototype.reduce = function(combiner, initialValue) {
      var counter,
        accumulatedValue;
      if (this.length === 0) {
        return this;
      else {
        if (arguments.length === 1) {
          counter = 1;
    ...
    
  7. reduce(combiner, initialValue)
    Array.prototype.reduce = function (combiner, initialValue) {
      var counter,
        accumulatedValue;
      if (this.length === 0) {
        return this;
      if (arguments.length === 1) {
        counter = 1;
        accumulatedValue = this[0];
    ...
    
  8. reduce(combiner, initialValue)
    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];
    ...
    
  9. reduce(f, firstValue)
    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);
    ...