Home > Article > Web Front-end > js implements moving up and down of data in an array
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) } }
Related recommendations:
How to realize the function of moving up and down the data in the list
Analysis of Java ordered array data structure and binary search algorithm
Please provide guidance on issues related to php array database
The above is the detailed content of js implements moving up and down of data in an array. For more information, please follow other related articles on the PHP Chinese website!