Home > Web Front-end > JS Tutorial > body text

Example of moving data up and down in an array using js

韦小宝
Release: 2018-01-08 10:53:15
Original
1723 people have browsed it

The following editor will bring you an example of js implementation of moving up and down data in an array. The editor thinks it’s pretty good, so I’ll share the js source code with you now and give it as a reference. If you are interested in js, please follow the editor to take a look.

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)
  }
}
Copy after login


The above example of js realizing moving data up and down in an array is all the content shared by the editor. I hope it can give you a reference, and I hope you will support PHP more. Chinese website.

Related recommendations:

JS simple method to move focus to the last position

Detailed explanation of js Dom to achieve skin change effect

JS Return to Top Example Sharing

The above is the detailed content of Example of moving data up and down in an array using js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template