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);
    },
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM