Nodejs Array Reduce reduce(f, firstValue)

Here you can find the source of reduce(f, firstValue)

Method Source Code

Array.prototype.reduce = function(f, firstValue){
   var value = firstValue;
   for (var i =0; i<this.length; i++){
      value = f(this[i], value);//from  ww w  .j  a va 2  s  . c  om
   }
   return value;
};
[1,2,3].reduce(function(currentValue ,value){return value+=currentValue;}, 0);
[1,2,3].reduce(function(currentValue ,value){return value*=currentValue;}, 0);

Related

  1. 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;
    ...
    
  2. 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;
    ...
    
  3. 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;
    ...
    
  4. 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];
    ...
    
  5. 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];
    ...
    
  6. 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;
    };
    ...
    
  7. 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;
    };
    ...
    
  8. reduce(f, value)
    Array.prototype.reduce = function (f, value) { 
        for (var i = 0; i < this.length; i++) { 
            value = f(this[i], value)
        return value
    
  9. reduce(f, value)
    Array.prototype.reduce = function (f, value) {
      for (var i = 0, len = this.length; i < len; i++) {
        value = f(this[i], value);
      return value;
    };
    var superheroes = ['superman', 'iron man', 'spiderman', 'batman'];
    var totalLength = superheroes.reduce(function (elem, acc) {
      return elem.length + acc;
    ...