Nodejs Array Insert insert(p_index, newObj)

Here you can find the source of insert(p_index, newObj)

Method Source Code

Array.prototype.insert = function(p_index, newObj){
   var l = this.length;
   var precObj = null;
   for(var i=0; i<l; i++){
      if(precObj != null){
         var obj = precObj;
         precObj = this[i];/*from  ww  w .  ja  va  2 s.  c  om*/
         this[i] = obj;
      }else if(i == p_index){
         precObj = this[i];
         this[i] = newObj;
      }
   }
   if(precObj != null){
      this.push(precObj);
   }else{
      this.push(newObj);
   }
};

Related

  1. insert(index, item)
    Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
    };
    Array.prototype.registerKeys = function(keys){
      for(var i=0;i< keys.length;i++){
        this.push(keys[i]);
    };
    Array.prototype.contains = function(item){
    ...
    
  2. insert(index, item)
    Array.prototype.insert = function(index, item) {
      index = Math.min(index, this.length);
      arguments.length > 1
        && this.splice.apply(this, [index, 0].concat([].pop.call(arguments)))
        && this.insert.apply(this, arguments);
      return this;
    
  3. insert(index, value)
    Array.prototype.insert = function(index, value)
      if(index >= 0)
        var a = this.slice(), b = a.splice(index);
        a[index] = value;
        return a.concat(b);
    };
    ...
    
  4. insert(index, value)
    Array.prototype.insert = function (index, value) {
        this.splice(index, 0, value);
    };
    
  5. insert(object)
    Array.prototype.insert = function(object) {
      this[this.length] = object;
      return this;
    
  6. insert(value)
    Array.prototype.insert = function(value) {
      if(this.root == null) {
        this.root[1] = value; 
    var heap = []; 
    heap.insert(30);
    console.log(heap);