Nodejs Array Inject inject(result, injectFunction)

Here you can find the source of inject(result, injectFunction)

Method Source Code

Array.prototype.inject = function(result, injectFunction) {
    this.each(function(element) {
        result = injectFunction(result, element);
    });//from   w w w .  j a  va  2 s. c om
    
    return result;
};

Related

  1. inject(element)
    var express = require('express')
      , app = express()
      , server = require('http').createServer(app)
      , io = require('socket.io').listen(server);
    server.listen(3000);
    app.use(express.static(__dirname + '/public'));
    var messages = new Array()
    Array.prototype.inject = function(element) {
        if (this.length >= 5) {
    ...
    
  2. inject(funct)
    Array.prototype.inject = function(funct) {
      var start = this.shift();
      var accum = start;
      var createInjection = function(num){
        if (accum = funct(accum, num));
      };
      this.myEach(createInjection);
      this.unshift(start);
      return accum;
    ...
    
  3. inject(index, val)
    Array.prototype.inject = function(index, val){
      if(index <0){return this;}
      var a = this.slice(0, index), b = this.splice(index, this.length-index);
      a[index] = val;
      return a.concat(b);
    };
    
  4. inject(memo, fn)
    Array.prototype.inject = function(memo, fn) {
      for(var i = 0; i < this.length; i++) {
        memo = fn(memo, i, this[i]);
      };
      return memo;
    };
    
  5. inject(memo, iterator, context)
    Array.prototype.inject = function(memo, iterator, context) {
        this.each(function(value, index) {
          memo = iterator.call(context, memo, value, index);
        });
        return memo;