Nodejs Array Reduce reduce(callback, initialValue)

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

Method Source Code

// https://developer.mozilla.org/en/New_in_JavaScript_1.8

// https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array/Reduce
// [1, 2, 3].reduce(function(sum, item) { return sum + item }, 0)
Array.prototype.reduce = function(callback, initialValue) {
  var last, returned;
  this.forEach(function(item, index, array) {
    if (index == 0 && initialValue) {
      returned = initialValue;//from ww w  . j av a 2s  .  c  om
    } else if (index == 1 && !initialValue) {
      returned = item;
    } else {
      last = returned, returned = null;
      returned = callback(last, item, index, array);
    };
  });
  return returned;
};

// functional-style
// ladit pres file:///Users/botanicus/Desktop/index.html
Array.prototype.reduce = function(callback, initialValue) {
  // var tail = this, head = initialValue || tail.shift(), initialValue = null;
  var head = initialValue || this.shift();
  var tail = this;
  var initialValue = null;
  if(tail) {
    tail.reduce(callback, head);
  } else {
    return head;
  }
};
[1, 2, 3].reduce(function(sum, item) { return sum + item })

Array.prototype.reduceRight

String.prototype.trim
String.prototype.trimLeft
String.prototype.trimRight

Related

  1. reduce()
    Array.prototype.reduce = function() {
      return this.length > 1 ? this : this[0];
    };
    
  2. reduce()
    var testArray1 = [1, 3, 5, 4, 2],
          sumArray = [];
    Array.prototype.reduce = function() {
      var sum = 0;
        for (i=0;i<this.length; ++i) {
        sum += this[i];
          return sumArray.push(sum);
    };
    ...
    
  3. reduce(aggregate, initial)
    Array.prototype.reduce = function(aggregate, initial) {
      const len = this.length;
      let startIndex = 0;
      if(typeof initial === 'undefined') {
        initial = this[0];
        startIndex = 1;
      for(let i = startIndex; i < len; i += 1) {
        initial = aggregate(initial, this[i], i, this);
    ...
    
  4. 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);
    ...
    
  5. 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;
    };
    ...
    
  6. 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;
    
  7. 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;
    ...
    
  8. 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;
    ...