Front-end processing array move up and down function

1. Encapsulate an array move up and down function move function():

   move(status, index, arr){ // status: 0-表示上移|1-表示下移;index表示当前下标; arr数组
        var data = JSON.parse(JSON.stringify(arr)); // 数组属于引入类型
        if (status == 0) {
                if(index != 0 ){ // 上移第一项无需上移
                  data.splice(index-1, 2, arr[index], arr[index-1]);
                } else{
                    console.log('已经是首项了')
                }
        } else if (status == 1) { // 下移最后一项无需下移
                if(index != arr.length){
                  data.splice(index, 2, arr[index+1], arr[index]);
                } else{
                    console.log('已经是最后一项了')
                }
        }
        return data
    },

2. As shown below (when the front end implements the following functions)

dropdown(i){ // 下移事件
  this.arr = this.mover(1, i, this.arr)
},

dropup(i) { // 上移事件
  this.arr = this.mover(0, i, this.arr)
}

Guess you like

Origin blog.csdn.net/m0_59910554/article/details/126425099