Javascript Array move(old_index, new_index)

Description

Javascript Array move(old_index, new_index)


Array.prototype.move = function(old_index, new_index) {
  if (new_index >= 0 && new_index < this.length)
  this.splice(new_index, 0, this.splice(old_index, 1)[0]);
  return this;/*from   w  w  w . j a v a  2s  .  c  om*/
};

Javascript Array move(old_index, new_index)

Array.prototype.move = function (old_index, new_index) {
  if (new_index >= this.length) {
    var k = new_index - this.length
    while ((k--) + 1) {
      this.push(undefined)//from w  w  w  . j ava2  s. co m
    }
  }
  this.splice(new_index, 0, this.splice(old_index, 1)[0])
  return this // for testing purposes
}

Javascript Array move(old_index, new_index)

Array.prototype.move = function (old_index, new_index) {
 while (old_index < 0) {
  old_index += this.length;// www .  j  a  v  a2  s  . c o  m
 }
 while (new_index < 0) {
  new_index += this.length;
 }
 if (new_index >= this.length) {
  var k = new_index - this.length;
  while ((k--) + 1) {
   this.push(undefined);
  }
 }
 this.splice(new_index, 0, this.splice(old_index, 1)[0]);
 return this; // for testing purposes
};

Javascript Array move(old_index, new_index)

Array.prototype.move = function (old_index, new_index) {
    while (old_index < 0) {
        old_index += this.length;
    }/*from ww  w  . j a v  a2  s  .  co  m*/
    while (new_index < 0) {
        new_index += this.length;
    }
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    //return this; // for testing purposes
};

Javascript Array move(old_index, new_index)

//Random things I've collected and need

//move things around in an array, useful for data bound arrays
Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);//from  w  ww . jav a  2  s.  c  om
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};

//hah!, sometimes you just want a regex to be text...
RegExp.escape = function(text) {
  return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};

Javascript Array move(old_index, new_index)

/**//w w  w.j a v a  2  s.c om
 * Moves array's element from old_index to new_index
 * @memberof module:ArrayFunctions
 * @param {number} old_index Old index
 * @param {number} new_index New index
 * @returns {Array}
 */
Array.prototype.move = function (old_index, new_index) {
    while (old_index < 0) {
        old_index += this.length;
    }
    while (new_index < 0) {
        new_index += this.length;
    }
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this;
};



PreviousNext

Related