Is there any simpler way to swap two elements in an array?(有没有更简单的方法来交换数组中的两个元素?)
var a = list[x], b = list[y]; list[y] = a; list[x] = b;
You only need one temporary variable.(您只需要一个临时变量。)
var b = list[y]; list[y] = list[x]; list[x] = b;
arr = [1,2,3,4]
[arr[0], arr[1]] = [arr[1], arr[0]];
[2,1,3,4]
1.4m articles
1.4m replys
5 comments
57.0k users