Home  >  Article  >  Web Front-end  >  Example analysis of moving data up and down in js array

Example analysis of moving data up and down in js array

小云云
小云云Original
2017-12-21 11:37:111607browse

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!

Statement:
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