Nodejs Array Reduce reduce(combiner, initialValue)

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

Method Source Code

// This implementation of the reduce function is copied from ReactiveX.io
// for self-learning and practicing application. 

Array.prototype.reduce = function(combiner, initialValue) {
  var counter,//  ww w . j  ava2s . com
    accumulatedValue;

  if (this.length === 1) {
    return this;
  } else {
      if (arguments.length === 1) {
        counter = 1;
        accumulatedValue = this[0];
      } else if (arguments >= 2) {
        counter = 0;
        accumulatedValue = initialValue;
      } else {
      throw "Invalid arguments.";
      }

      while (counter < this.length) {
        accumulatedValue = combiner(accumulatedValue, this[counter]);
        counter++;
      }

    return [accumulatedValue];
  }
}

var ratings = [2,1,5,3,4];

ratings.reduce(function(prev, curr) {
  if (curr > prev) {
    return prev = curr;
  } else {
    return prev;
  }
});

// My original written version adapted from Eloquent JavaScript:

function reduce(array, combine, start) {
  array.each(function(item) {
    start = combine(start, item);
  });
  return start;
}

Array.prototype.each = function(collection, callback) {
  var i, len, key;
  if (Array.isArray(collection)) {
    for (i = 0, len = collection.length; i < len; i += 1) {
      callback(collection[i]);
    } 
  } else {
    for (key in collection) {
      callback(collection[key]);
    }
  }
};

Related

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