Swapping two items in a javascript array [duplicate]

The return value from a splice is the element(s) that was removed-

no need of a temp variable

Array.prototype.swapItems = function(a, b){
    this[a] = this.splice(b, 1, this[a])[0];
    return this;
}

var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

alert(arr.swapItems(3, 7));

returned value: (Array)

    0,1,2,7,4,5,6,3,8,9

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)