Insert element to array - Node.js Array

Node.js examples for Array:Insert

Description

Insert element to array

Demo Code


Array.prototype.insertFrom = function(from, to){
  to       = Math.max(0, to);/*from   w  w  w .  j av a2  s .  c o  m*/
  from     = Math.min( Math.max(0, from), this.length-1 );
    
  var el     = this[from];
  var old   = this.without(el);
  var newA   = old.slice(0, to);
  newA.push(el);
  if(old.length > to ){
    newA   = newA.concat(old.slice(to))
  };
  return newA;
}

Related Tutorials