js 实现数组元素上移、下移

  • Post author:
  • Post category:其他


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



版权声明:本文为weixin_38673922原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。