Nodejs Array Reduce reduce.call(str, (trav, char)>

Here you can find the source of reduce.call(str, (trav, char)>

Method Source Code

'use strict';//from w  w w  .  j av a2  s  . c  o  m

class PrefixTree {
    constructor() {
        this.root = {};
    }

    insert(str) {
        Array.prototype.reduce.call(str, (trav, char) => {
            if (!trav[char]) {
                trav[char] = {};
            } 

            return trav[char];
        }, this.root);
    }


}

let tree = new PrefixTree();
tree.insert('hello');
tree.insert('health');
console.log(tree);

function reduce(arr, fn, initialValue) {
    for (var i = 0;i < arr.length;i++) {

    }

}

Array.prototype.tonysReduce =  (fn, initialValue) => {
    console.log('this: ', this);
    for (let i = 0;i < this.length;i++) {
        console.log(initialValue);
        initialValue = fn(initialValue, this[i], i, this)
    }

    return initialValue;
};

let arr = [1,2,3,4];

let val = arr.tonysReduce((initialValue, num) => {
    return initialValue + num;
}, 0);

console.log(val);

Related

  1. reduce(reducingFunction, initialValue)
    Array.prototype.reduce = function(reducingFunction, initialValue) {
      var result = initialValue;
      this.forEach(function(x) {
        reducingFunction(result, x)
      });
      return result;
    
  2. reduce(t, fn)
    Array.prototype.reduce = function(t, fn) {
      for(var i = 0 ; i < this.length ; i++) {
        t = fn(t, this[i])
      return t
    
  3. 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){
    ...
    
  4. 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){
    ...
    
  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. 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);
    ...
    
  7. 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;
    ...
    
  8. reduceRight2(pasteback, initial)
    Array.prototype.reduceRight2 = function(pasteback, initial){
      for(var i = this.length
          , value = arguments.length>1 ? initial : this[--i]
      ; i--;){
        value = pasteback(value, this[i], i, this);
      return value;
    };
    
  9. _reduce(f)
    Array.prototype._reduce = function(f){
        let acc = this[0];
        for(let i = 1; i < this.length; i++){
            acc = f(acc,this[i])
        return acc;