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

Example analysis of moving data up and down in js array

小云云
Release: 2017-12-21 11:37:11
Original
1591 people have browsed it

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

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!