调换数组中3和4的位置,也就是下标为2,3的位置
var arr = [1,2,3,4,5,6];
[arr[2], arr[3]] = [arr[3], arr[2]];
console.log(arr); //[1, 2, 4, 3, 5, 6]
var t = arr[2];
arr[2] =arr[3];
arr[3] = t
console.log(arr) //[1, 2, 4, 3, 5, 6]
arr[3] = [arr[2],arr[2]=arr[3]][0]
console.log(arr) //[1, 2, 4, 3, 5, 6]
arr.splice(2, 1, ...arr.splice(3, 1, arr[2]))
//[...iterator]得到原数组的索引,比如[...[6,6,6]]会输出[6,6,6]
console.log(arr);//[1, 2, 4, 3, 5, 6]
网友评论