Nodejs Array Reduce reduce(f, value)

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

Method Source Code

// 6_5_Confusion/*from  w  ww . j  a v  a 2  s .co  m*/
var is_array_01 = function (value) {
   return value &&
      typeof value === 'object' &&
      value.constructor === Array;
};

var is_array = function (value) {
   return value &&
      typeof value === 'object'  &&
      typeof value.length === 'number'  &&
      typeof value.splice === 'function'  &&
      !(value.propertyIsEnumerable('length'));
};

// 6_6_Methods
// Array.method('reduce', function(f, value){
Array.prototype.reduce = function(f, value){
   var i;
   for (i = 0; i < this.length; i++) {
      value = f(this[i],value);
   }
   return value;
};

// 6_7_dim
Array.dim = function(dimension, initial) {
   var a = [], i;
   for (i = 0; i < dimension; i++) {
      a[i] = initial;
   }
   return a;
};

// 6_7_matrix
Array.matrix = function (m, n, initial) {
   var a, i, j, mat = [];
   for (i = 0; i < m; i += 1) {
      a = [];
      for (j = 0; j < n; j += 1) {
         a[j] = initial;
      }
      mat[i] = a;
   }
   return mat;
};

Related

  1. 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;
    };
    ...
    
  2. 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;
    };
    ...
    
  3. reduce(f, value)
    Array.prototype.reduce = function (f, value) { 
        for (var i = 0; i < this.length; i++) { 
            value = f(this[i], value)
        return value
    
  4. 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;
    ...
    
  5. reduce(f, value)
    var data = [ 4, 8, 15, 16, 23, 42 ];
    Array.prototype.reduce = function(f, value) {
        var i;
        for (i = 0; i < this.length; i += 1) {
            value = f(this[i], value);
        return value;
    };
    var add = function(a, b) {
    ...
    
  6. reduce(f, value)
    Array.prototype.reduce = function(f, value) {
        var i;
        for (i = 0; i < this.length; i += 1) {
            value = f(this[i], value);
        return value;
    };
    var data = [ 4, 8, 15, 16, 23, 42 ];
    var add = function(a, b) {
    ...
    
  7. reduce(fn, initial)
    Array.prototype.reduce = function(fn, initial)
        var r, i;
        initial ? r = initial : r = this[0];
        for (i=1;i<this.length;i++)
            r = fn(r, this[i]);
        return r;
    ...
    
  8. reduce(fn, initialValue)
    Array.prototype.reduce = function (fn, initialValue) {
      let acc = initialValue
      for (var i = 0; i < this.length; i++) {
        acc = fn(acc, this[i], i)
      return acc
    Array.prototype.forEach = function (fn) {
      this.reduce((acc, curr, i) => {
    ...
    
  9. reduce(fun /* , initial */)
    if (!Array.prototype.reduce) {
      Array.prototype.reduce = function(fun ) {
        var len = this.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
        if (len == 0 && arguments.length == 1)
          throw new TypeError();
        var i = 0;
        if (arguments.length >= 2) {
    ...