JS implements array element Exchange location

Source: Internet
Author: User

/**
* Array element Exchange location
* @param {array} arr arrays
* @param {Number} index1 where to add the project
* @param {Number} Index2 delete the location of the project
* Index1 and Index2 are the index values of two arrays, that is, two index values to swap the position of the elements, such as 1,5 is the two elements in the array labeled 1 and 5 exchange positions
*/
function Swaparray (arr, index1, Index2) {
ARR[INDEX1] = Arr.splice (index2, 1, arr[index1]) [0];
return arr;
}

Move up places the current array index index with the next element, moving one after the array

function Zindexup (arr,index,length) {
if (index+1! = length) {
Swaparray (arr, index, index+1);
}else{
Alert (' already pinned, unable to move up ');
}
}

Move down places the current array index index in place of the previous one, moving one in front of the array

function Zindexdown (arr,index,length) {
if (index!= 0) {
Swaparray (arr, index, index-1);
}else{
Alert (' Already in the bottom, unable to move Down ');
}
}

Pinned, moving the current element to the last of the array

function Zindextop (arr,index,length) {
if (index+1! = length) {

First, the current element needs to be moved up several positions, the bottom is moved to the first bit of the array
var movenum = length-1-index;

Loop out the number of times it takes one to move up
for (var i = 0; i<movenum; i++) {

Swaparray (arr, index, index + 1);

index++;

}
}else{
Alert (' already pinned ');
}
}

Bottom, moves the current element to the first bit of the array

function Zindexbottom (arr,index,length) {
if (index!=0) {

First, the current element needs to be moved up several positions, the bottom is moved to the first bit of the array
var movenum = index-0;

Loop out the number of times it takes one to move up
for (var i = 0; i<movenum; i++) {

Swaparray (arr, index, index-1);

index--;

}
}else{
Alert (' Already in the bottom ');
}
}

JS implements array element Exchange location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.