Nodejs Array Reduce reduce(testFunction,initialValue)

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

Method Source Code

Array.prototype.reduce = function(testFunction,initialValue){
   var accumulatedValue,counter;
   if(this.length == 0){
      return this;
   }else{/* w  w w  .  ja  v  a2 s.  c o m*/
      if(arguments.length == 1){
         counter = 1;
         accumulatedValue = this[0];
      }else if(arguments.length >= 2){
         counter = 0;
         accumulatedValue = initialValue;
      }else{
         throw 'Invalid arguments.'
      }

      while(counter < this.length){
         accumulatedValue = testFunction(accumulatedValue,this[counter]);
         console.log(accumulatedValue);
         counter++;
      }
      return [accumulatedValue];
   }

}


Array.prototype.map = function(testFunction){
   var result = [];
   this.forEach(function(item){
      result.push(testFunction(item));
   })
   return result;
}


(function(){
   var videos = [
      {
         "id": 65432445,
         "title": "The Chamber"
      },
      {
         "id": 675465,
         "title": "Fracture"
      },
      {
         "id": 70111470,
         "title": "Die Hard"
      },
      {
         "id": 654356453,
         "title": "Bad Boys"
      }
   ];

   return videos.
      reduce(function(acc,curr){
         var acc_copy = Object.create(acc);
         acc_copy[curr.id] = curr.title;
         return acc_copy;
      },{})
}());

Related

  1. reduce(process, initial)
    Array.prototype.reduce = function(process, initial) {
      var value = initial;
      for (var i = 0; i < this.length; i++) {
        if( ( i === 0 ) && ( value === undefined || value === null ))
            value = this[0];
            continue;
        value = process( value, this[i] );
    ...
    
  2. reduce(process, initial)
    Array.prototype.reduce = function(process, initial) {
      if (initial === undefined) {
        var slice = this.slice(1);
        initial = this[0];
      var array = slice || this;
      array.forEach(function(value) {
        initial = process(initial, value);
      });
    ...
    
  3. reduce(reducingFunction, initialValue)
    Array.prototype.reduce = function(reducingFunction, initialValue) {
      var result = initialValue;
      this.forEach(function(x) {
        reducingFunction(result, x)
      });
      return result;
    
  4. reduce(t, fn)
    Array.prototype.reduce = function(t, fn) {
      for(var i = 0 ; i < this.length ; i++) {
        t = fn(t, this[i])
      return t
    
  5. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  6. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  7. reduce.call(str, (trav, char)>
    'use strict';
    class PrefixTree {
        constructor() {
            this.root = {};
        insert(str) {
            Array.prototype.reduce.call(str, (trav, char) => {
                if (!trav[char]) {
                    trav[char] = {};
    ...
    
  8. reduceRight(aggregate, initial)
    Array.prototype.reduceRight = function(aggregate, initial) {
      const len = this.length;
      let startIndex = len - 1;
      if(typeof initial === 'undefined') {
        initial = this[0];
        startIndex = len - 2;
      for(let i = startIndex; i >= 0; i -= 1) {
        initial = aggregate(initial, this[i], i, this);
    ...
    
  9. reduceRight(fun /*, initial*/)
    Array.prototype.reduceRight = 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 = len - 1;
    ...