Nodejs Array Move Up moveUp(value, by)

Here you can find the source of moveUp(value, by)

Method Source Code

Array.prototype.moveUp = function(value, by) {
    var index = this.indexOf(value),
        newPos = index - (by || 1);/* w w  w  . ja v a  2 s .c o  m*/
    
    if(index === -1)
        throw new Error("Element not found in array");
    
    if(newPos < 0)
        newPos = 0;
        
    this.splice(index,1);
    this.splice(newPos,0,value);
    
    return newPos;
};

Related

  1. moveUp(element, offset)
    Array.prototype.moveUp = function(element, offset) 
        var index = this.indexOf(element);    
        var newPos = index - (offset || 1);
        if(index === -1) { throw new Error("Element not found in array"); }
        if(newPos < 0) { newPos = 0; }
        this.splice(index,1);
        this.splice(newPos,0,element);
    };
    ...
    
  2. moveUp(value, by)
    Array.prototype.moveUp = function(value, by) {
      var index = this.indexOf(value),
        newPos = index - (pos || 1);
      if (index === -1) {
        throw new Error('Item not found');
      if (newPos < 0) {
        newPos = 0;
      this.splice(index,1);
      this.splice(newPos, 0, value);