Nodejs Array Insert insert(index, item)

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

Method Source Code

Array.prototype.insert = function (index, item) {
  this.splice(index, 0, item);//  ww  w. ja va2 s .c  om
};

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) {
        return sorted.insert(j+1, obj);
      }
      if(!j) sorted.unshift(obj);
    }
  }

  for(var i=0; i<array.length; i++) backItUp(array[i]);
  return sorted;
}

Related

  1. insert(index, item)
    Array.prototype.insert = function (index, item) {
      if (index)
        this.splice(index, 0, item);
      else
        this.push(item);
    };
    
  2. insert(index, item)
    Array.prototype.insert = function (index, item) {
      this.splice(index, 0, item);
    
  3. insert(index, item)
    Array.prototype.insert = function(index, item) {
      this.splice(index, 0, item);
      return this;
    };
    
  4. 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];
      });
    
  5. 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);
    ...
    
  6. 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){
    ...
    
  7. 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;
    
  8. 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);
    };
    ...
    
  9. insert(index, value)
    Array.prototype.insert = function (index, value) {
        this.splice(index, 0, value);
    };