Javascript Array moveUp(element, offset)

Description

Javascript Array moveUp(element, offset)


// Array Shifting
Array.prototype.moveUp = function(element, offset) 
{
    var index = this.indexOf(element);    
    var newPos = index - (offset || 1);
    /*from  www.  jav a  2 s .co  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,element);
};



PreviousNext

Related