This article mainly brings you an example of js realizing moving up and down data in an array. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.
The example is as follows:
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) } }
If you want to close the recommendation:
JS implementation of clicking up and down to move LI row data example explanation
jQuery's method of moving up and down based on the sorting function
How to implement the move down and move up function of data in the list
The above is the detailed content of Example analysis of moving data up and down in js array. For more information, please follow other related articles on the PHP Chinese website!