参考:
上移、下移、置顶:https://blog.csdn.net/Chou_Junn/article/details/84967408
JavaScript 交换数组元素位置的几种方式:https://www.cnblogs.com/shapeY/p/9669924.html
数组元素互换方法:
第三方变量
let temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
不使用变量,加减法(也可以用乘除法)
public static void swap(int[] a, int i, int j){
a[i] = a[i] + a[j];
a[j] = a[i] - a[j]; // a[j] = a[i] + a[j] - a[j]
a[i] = a[i] - a[j]; // a[i] = a[i] + a[j] - a[i]
}
splice
function swapArr(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
解构赋值
[array[index1],array[index2]] = [array[index2],array[index1]];
代码中使用的方法
/**
* 增加case\then
*/
addCase() {
this.expressionList.push({case: "", then: ""});
}
/**
* 删除case\then
*/
delete(index) {
if (this.expressionList.length == 1) return;
this.expressionList.splice(index, 1);
}
/**
* 上移case\then
*/
up(index) {
if (index == 0) return;
this.swopItems(this.expressionList, index, index - 1)
}
/**
* 下移case\then
*/
down(index) {
if (index == this.expressionList.length - 1) return;
this.swopItems(this.expressionList, index, index + 1)
}
/**
* 交换数组元素
*/
swopItems(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
};