Nodejs Array Inject inject(index, val)

Here you can find the source of inject(index, val)

Method Source Code

/**//  w w w.j  a v  a 2s. c  om
@Name: Array.prototype.inject
@Author: Paul Visco
@Version: 1.1 11/19/07
@Description: Finds the index of the value given within the array.  Return the position of the first matching value.  Rememeber that array start at 0.
@Param: Object/String/Number val The value to inject into the array. 
@Return: Integer
@Example:
var myArray = ['zero', 'one', 'two'];
var answer = myArray.inject(1, 'bagel');

//answer is now ['zero', 'bagel', 'one', 'two'];
*/
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);
};

Related

  1. inject
    ===
    
  2. inject(callback)
    Array.prototype.inject = function (callback) {
      var accumulator = this[0];
      var body = this.slice(1, this.length);
      body.each(function (el) {
        accumulator = callback(accumulator, el);
      });
      return accumulator;
    };
    var add = function (a, el) {
    ...
    
  3. inject(command, accumulator)
    Array.prototype.inject = function(command, accumulator){
      var start = 0;
      if(!accumulator){
        accumulator = this[0]
        start += 1;
      this.slice(start,this.length).each(function(el){
        accumulator = command(accumulator, el);
      });
    ...
    
  4. 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) {
    ...
    
  5. 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;
    ...
    
  6. 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;
    };
    
  7. 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;
    
  8. inject(result, injectFunction)
    Array.prototype.inject = function(result, injectFunction) {
        this.each(function(element) {
            result = injectFunction(result, element);
        });
        return result;
    };