Nodejs Array Value iReduce(handler, initialValue)

Here you can find the source of iReduce(handler, initialValue)

Method Source Code

var ary = [0,1,2,3,4,5,6,7,8,9];
var initialValue;

Array.prototype.iReduce = function(handler, initialValue) {
   if (typeof handler !== 'function') {
      throw new Error(handler + 'is not a function!');
   }/*w w w .ja  v  a2 s  .co m*/
   var ary = this;
   var i = 0;
   var tempCurrent = initialValue;
   // ??initialValue,?????
   if (arguments.length === 1) {
      i = 1;
      tempCurrent = ary[0];
   }

   for (; i<ary.length; i++) {
      tempCurrent = handler(tempCurrent, ary[i], i, ary);
   }
   return tempCurrent;
}

var newAry = ary.iReduce(function(previous, current, index, ary) {
   console.log(previous, current, index, ary);
   //console.log(this);
   //console.log('======================');
   return previous + current;
}, initialValue);
console.log(newAry);

Related

  1. defaultIfEmpty(val)
    Array.prototype.defaultIfEmpty = function (val) {
      return this.length == 0 ? [val == null ? null : val] : this;
    };
    
  2. elementWithAttr(attr, value)
    Array.prototype.elementWithAttr = function (attr, value) {
        for(var i = 0; i < this.length; i += 1) {
            if(this[i][attr] === value) {
                return this[i];
        return null;
    };
    
  3. first(attribut, value)
    Array.prototype.first = function (attribut, value) {
        for (var i = 0; i < this.length; i++) {
            if (this[i][attribut] == value)
                return this.slice(i, i + 1)[0];
        return null;
    };
    
  4. getNextValue(index)
    Array.prototype.getNextValue = function(index) {
      let nextIndex = index + 1;
      if(nextIndex < this.length) {
        return this[nextIndex];
      return null;
    
  5. hasWhere(property, value)
    Array.prototype.hasWhere = function (property, value) {
        "use strict";
        for(var i = 0; i < this.length; i++) {
            if(this[i][property] === value) {
                return true;
        return false;
    };
    ...
    
  6. makeArray(value){
    Array.makeArray = function(value){
      if ( !value ) return [];
      if ( typeof value == "string" ) return [value];
      return Array.prototype.slice.call(value, 0);
    };
    
  7. nIndexOf(value)
    Array.prototype.nIndexOf = function(value){
        var firstIndex = 0,
            lastIndex = this.length - 1,
            middleIndex = Math.floor((lastIndex + firstIndex) / 2);
        while (this[middleIndex] != value && firstIndex < lastIndex) {
            if (value < this[middleIndex]) {
                lastIndex = middleIndex - 1;
            } else if (value > this[middleIndex]) {
                firstIndex = middleIndex + 1;
    ...
    
  8. oneValue()
    Array.prototype.oneValue = function()
        var max = this[0];
        for(el in this)
            if(this[el] !== max && !isNaN(this[el]))
                return false;
        return true;
    };
    
  9. pad(minSize, val)
    Array.prototype.pad = function(minSize, val) {
        var padded = new Array();
        for (var i = 0; i < this.length; i++) {
            padded.push(this[i]);
        while (padded.length < minSize) {
            padded.push(val || null);
        return padded;
    ...