Nodejs Array Insert insert(i, ob)

Here you can find the source of insert(i, ob)

Method Source Code

Array.prototype.insert = function(i, ob) {
    i = i - 1;/*from w w w .  j  a  v a2 s .  c o  m*/
    if (i < 0)i = 0;
    if (i > this.length)i = this.length;
    var st = (i == 0) ? [] : this.slice(0, i);
    st.push(ob);
    var ed = (i >= this.length) ? [] : this.slice(i);
    return st.concat(ed);
};

Related

  1. insert( i, v )
    Array.prototype.insert = function( i, v ) {
      if (this.length == 0)
        return [v];
      if( i>=0 ) {
        var a = this.slice(), b = a.splice( i );
        a[i] = v;
        return a.concat( b );
    };
    ...
    
  2. insert(elem, pos)
    Array.prototype.insert = function(elem, pos) {
        this.splice(pos, 0, elem);
    
  3. insert(element)
    Array.prototype.insert = function(element) {
      if (this.indexOf(element) === -1) {
        this.push(element);
      return this;
    };
    
  4. insert(idx, item)
    Array.prototype.insert = function(idx, item) {
      this.splice(idx, 0, item);
    };
    
  5. insert(index , item)
    Array.prototype.insert = function(index , item){
        this.splice(index, 0, item);
    };
    
  6. insert(index)
    Array.prototype.insert = function(index) {
        this.splice.apply(this, [index, 0].concat(
            Array.prototype.slice.call(arguments, 1)));
        return this;
    };
    
  7. insert(index)
    Array.prototype.insert = function(index) {
        function isArray(instance) {
            return Object.prototype.toString.call(instance) !== '[object Array]';
        if (!isArray(this)) {
            throw new TypeError("`this` must be Array, not " + typeof this);
        if (typeof index !== 'number') {
            throw new TypeError("arguments[0] must be number (index to insert at), not " + typeof arguments[0]);
    ...