- splice——替换功能,第一个参数(起始位置),第二个参数(删除的项数),第三个参数(插入任意数量的项)。
- 用法:array.splice(index,num,insertValue),返回值为删除内容,array为结果值。
moveUp(index) {if (index === 0) {return}util.moveUp(this.templates, index)},moveDown(index) {if (index === (this.templates.length - 1)) {return}util.moveDown(this.templates, index)}
swapArray(arr, index1, index2) {console.log('数组', arr)console.log('原下标', index1) // 1console.log('新下标', index2) // 0arr[index1] = arr.splice(index2, 1, arr[index1])[0]console.log('操作', arr[index1])return arr},// 上移 将当前数组index索引与后面一个元素互换位置,向数组后面移动一位moveUp(arr, index) {this.swapArray(arr, index, index - 1)},// 下移 将当前数组index索引与前面一个元素互换位置,向数组前面移动一位moveDown(arr, index) {this.swapArray(arr, index, index + 1)},
如何实现换位? 1 、2 、3(index2) 、4(index1)===> 1 2 4 3;
- arr.splice(index2, 1, arr[index1])[0] ====> 1 、2 、4 、4 ====> return 返回被删除的结果 “3”
- 数组第四个 arr[index1] = 3
- 1244 == >1243
