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  w  ww  .j a v  a 2s.co m
      accumulatedValue;

   // 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;
         accumulatedValue = this[0];
      }
      else if (arguments.length >= 2) {
         counter = 0;
         accumulatedValue = 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) {
         accumulatedValue = combiner(accumulatedValue, this[counter])
         counter++;
      }

      return [accumulatedValue];
   }
};

Related

  1. 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;
    ...
    
  2. 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;
    };
    ...
    
  3. 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;
    
  4. 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;
    ...
    
  5. reduce(combiner, initialValue)
    Array.prototype.reduce = function(combiner, initialValue) {
      var counter,
        accValue;
      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;
      if (arguments.length === 1) {
        counter = 1;
        accumulatedValue = this[0];
    ...
    
  7. 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];
    ...
    
  8. 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);
    ...
    
  9. reduce(f, inital)
    var arr = [0,1,2,3,4];
    var sum = function(x, y) { return x + y; };
    Array.prototype.reduce = function(f, inital) {
        var acc = initial;
        for (var i = 0; i < this.length; i++) {
            acc = f(acc, this[i]);
        return acc;
    };
    ...