全国统一服务热线:400-633-9193

js实现数组内数据的上移和下移的实例

    网络     2018-01-05    1560

实例如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var swapItems = function(arr, index1, index2){
  arr[index1] = arr.splice(index2,1,arr[index1])[0]
  return arr
}
var arr = [1,2,3]
var newArr = []
upData (arr, index) {
  if (this.arr.length > 1 && index !== 0) {
    newArr = swapItems(arr, index, index - 1)
  }
}
downData (arr, index) {
  if (this.arr.length > 1 && index !== (this.arr.length - 1)) {
    newArr = swapItems(this.arr, index, index + 1)
  }
}


  分享到:  
0.2498s