Home > Web Front-end > JS Tutorial > js implements moving up and down of data in an array

js implements moving up and down of data in an array

小云云
Release: 2018-01-19 13:07:30
Original
1970 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

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!

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