# JavaScript交换数组元素

``var a = list[x], b = list[y]; list[y] = a; list[x] = b;` `

### 20 Solutions collect form web for “JavaScript交换数组元素”

` `var b = list[y]; list[y] = list[x]; list[x] = b;` `

` `var A = [1, 2, 3, 4, 5, 6, 7, 8, 9], x= 0, y= 1; A[x] = A.splice(y, 1, A[x])[0]; alert(A); // alerts "2,1,3,4,5,6,7,8,9"` `

` `var b = list[y]; list[y] = list[x]; list[x] = b;` `

` `var b = list[y];` `

` `Array.prototype.swap = function (x,y) { var b = this[x]; this[x] = this[y]; this[y] = b; return this; }` `

` `list.swap( x, y )` `

` `var tmp = list[x]; list[x] = list[y]; list[y] = tmp;` `

` `list[x] = list[x] ^ list[y]; list[y] = list[y] ^ list[x]; list[x] = list[x] ^ list[y];` `

` `list[x] = list[x] + list[y]; list[y] = list[x] - list[y]; list[x] = list[x] - list[y];` `

` `list[x] = [list[y],list[y]=list[x]][0]` `

` `list = [1,2,3,4,5] list[1] = [list[3],list[3]=list[1]][0] //list is now [1,4,3,2,5]` `

` `var a=1,b=5; a = [b,b=a][0]` `

Metafilter的一位随机人士表示 ：“Javascript的最新版本让你可以更加巧妙地进行掉换 （除其他外）：”

` `[ list[x], list[y] ] = [ list[y], list[x] ];` `

` `var a = 5, b = 9; b = (a += b -= a) - b; alert([a, b]); // alerts "9, 5"` `

` `array.splice(IndexToSwap,2,array[IndexToSwap+1],array[IndexToSwap]);` `

` `var swap = function (x){return x}; b = swap(a, a=b); c = swap(a, a=b, b=c);` `

` `var swap = function (x){return x}; list[y] = swap(list[x], list[x]=list[y]);` `

` `var a = [1,2,3,4,5], b=a.length; for (var i=0; i<b; i++) { a.unshift(a.splice(1+i,1).shift()); } a.shift(); //a = [5,4,3,2,1];` `

` `var a = 1, b = 2; [a, b] = [b, a]; --> a = 2, b = 1` `

### 对于两个或更多元素（固定号码）

` `[list[y], list[x]] = [list[x], list[y]];` `

### 对于可变数量的元素

` `function multiswap(arr,i0,i1) {/* argument immutable if string */ if (arr.split) return multiswap(arr.split(""), i0, i1).join(""); var diff = []; for (let i in i0) diff[i0[i]] = arr[i1[i]]; return Object.assign(arr,diff); } Example: var alphabet = "abcdefghijklmnopqrstuvwxyz"; var [x,y,z] = [14,6,15]; var output = document.getElementsByTagName("code"); output[0].innerHTML = alphabet; output[1].innerHTML = multiswap(alphabet, [0,25], [25,0]); output[2].innerHTML = multiswap(alphabet, [0,25,z,1,y,x], [25,0,x,y,z,3]);` `
` `<table> <tr><td>Input:</td> <td><code></code></td></tr> <tr><td>Swap two elements:</td> <td><code></code></td></tr> <tr><td>Swap multiple elements:&nbsp;</td> <td><code></code></td></tr> </table>` `

` `var a = 1; var b = 2; [a,b] = [b,a];` `

（ES6方式）

` `arr.slice(0,i1).concat(arr[i2],arr.slice(i1+1,i2),arr[i1],arr.slice(i2+1))` `

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

` `var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // swap index 0 and 2 arr[arr.length] = arr[0]; // copy idx1 to the end of the array arr[0] = arr[2]; // copy idx2 to idx1 arr[2] = arr[arr.length-1]; // copy idx1 to idx2 arr.length--; // remove idx1 (was added to the end of the array) console.log( arr ); // -> [3, 2, 1, 4, 5, 6, 7, 8, 9]` `

`let newList = Object.assign([], list, {[x]: list[y], [y]: list[x]})`

（当问题发布时，使用语言function在2009年不可用！）

` `array.unshift( array.pop() );` `

` `Array.prototype.swap = function(a, b) { var temp = this[a]; this[a] = this[b]; this[b] = temp; };` `

` `var myArray = [0,1,2,3,4...]; myArray.swap(4,1);` `
• Javascript将数组值推入另一个数组
• 在bash中同时迭代两个数组
• 从javascript中的对象数组中删除重复项
• 如何将命令的输出分配给数组？
• 你如何检查一个variables是否是JavaScript中的数组？
• 原始数组是以堆栈还是堆存储的？
• 如何从数组中获得随机值
• 如何修剪数组中的所有string？
• 将值添加到C＃数组
• 检查数组是否为空
• array_push（）和\$ array =之间的区别