js实现上移和下移

class Utils {
    swapArray(arr, index1, index2) {
      arr[index1] = arr.splice(index2, 1, arr[index1])[0];
      return arr;
    },

    // 上移 将当前数组index索引与后面一个元素互换位置,向数组后面移动一位
    moveUp(arr, index) {
      this.swapArray(arr, index, index - 1);
    },

    // 下移 将当前数组index索引与前面一个元素互换位置,向数组前面移动一位
    moveDown(arr, index) {
      this.swapArray(arr, index, index + 1);
    },
}

  

猜你喜欢

转载自www.cnblogs.com/xuanbingbingo/p/10192370.html