Nodejs Array Insert insert(index, value)

Here you can find the source of insert(index, value)

Method Source Code

/**// www.  ja  v  a 2 s  .  c o m
 * insert is a method of Array to insert value at index, without overwriting existing keys
 *
 * @param index       a Number, index where insert element
 * @param value       a Object (String, Number, ...), element to insert
 * @return            a Array, the array with the element inserted;
 */
Array.prototype.insert = function(index, value)
{
  if(index >= 0)
  {
    var a = this.slice(), b = a.splice(index);
    a[index] = value;
    return a.concat(b);
  }
};

Related

  1. insert(index, item)
    Array.prototype.insert = function(index, item) {
      this.splice(index, 0, item);
      return this;
    if (Function.prototype.name === undefined){
      Object.defineProperty(Function.prototype,'name',{
        get: function(){
          return /function ([^(]*)/.exec( this+"" )[1];
      });
    
  2. insert(index, item)
    Array.prototype.insert = function(index, item) {
      var args = this.slice.call(arguments),
          index = args.shift();
      args = [index,0].concat(args);
      this.splice.apply(this, args);
      return this;
    var result = [1,2,3,4,7,8,9,10].insert(4,5,6);
    console.log(result);
    ...
    
  3. insert(index, item)
    Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
    };
    function insertionSort (array) {
      var sorted = [];
      function backItUp(obj) {
        if(!sorted.length) return sorted.push(obj);
        for(var j=sorted.length-1; j>=0; j--) {
          if(obj.value >= sorted[j].value) {
    ...
    
  4. 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){
    ...
    
  5. 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;
    
  6. insert(index, value)
    Array.prototype.insert = function (index, value) {
        this.splice(index, 0, value);
    };
    
  7. insert(object)
    Array.prototype.insert = function(object) {
      this[this.length] = object;
      return this;
    
  8. insert(p_index, newObj)
    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];
          this[i] = obj;
        }else if(i == p_index){
    ...
    
  9. insert(value)
    Array.prototype.insert = function(value) {
      if(this.root == null) {
        this.root[1] = value; 
    var heap = []; 
    heap.insert(30);
    console.log(heap);