zoukankan      html  css  js  c++  java
  • js 实现数组元素交换位置

    /**
    * 数组元素交换位置
    * @param {array} arr 数组
    * @param {number} index1 添加项目的位置
    * @param {number} index2 删除项目的位置
    * index1和index2分别是两个数组的索引值,即是两个要交换元素位置的索引值,如1,5就是数组中下标为1和5的两个元素交换位置
    */
    function swapArray(arr, index1, index2) {
       arr[index1] = arr.splice(index2, 1, arr[index1])[0];
        return arr;
    }
    
    //上移 将当前数组index索引与后面一个元素互换位置,向数组后面移动一位
    
    function zIndexUp(arr,index,length){
       if(index+1 != length){
           swapArray(arr, index, index+1);
       }else{
          alert(‘已经处于置顶,无法上移‘);
      }
    }
    
    //下移 将当前数组index索引与前面一个元素互换位置,向数组前面移动一位
    
    function zIndexDown(arr,index,length){
       if(index!= 0){
           swapArray(arr, index, index-1);
       }else{
          alert(‘已经处于置底,无法下移‘);
      }
    }
    
    //置顶,即将当前元素移到数组的最后一位
    
    function zIndexTop(arr,index,length){
       if(index+1 != length){
    
         //首先判断当前元素需要上移几个位置,置底移动到数组的第一位
          var moveNum = length - 1 - index;
    
          //循环出需要一个一个上移的次数
           for (var i = 0; i<moveNum; i++) {
    
             swapArray(arr, index, index + 1);
    
              index++;
    
         }
       }else{
          alert(‘已经处于置顶‘);
      }
    }
    
    //置底,即将当前元素移到数组的第一位
    
    function zIndexBottom(arr,index,length){
       if(index!=0){
    
          //首先判断当前元素需要上移几个位置,置底移动到数组的第一位
          var moveNum = index - 0;
    
          //循环出需要一个一个上移的次数
           for (var i = 0; i<moveNum; i++) {
    
             swapArray(arr, index, index - 1);
    
              index--;
    
         }
       }else{
          alert(‘已经处于置底‘);
      }
    }
  • 相关阅读:
    ALhoViMwaR
    lenovo anti-virus powered by Intel security保护已过期
    python Module turtle has no circle member解决办法
    为什么电脑打出来都是繁体字
    python 学习笔记(七)(函数)
    python 写汉诺塔移动过程
    python 学习笔记(六)(dict,set)
    Vue CLI组件循环引用踩坑,组件未注册问题
    vue package.json脚本赋值变量
    linux服务器安装node环境及项目部署
  • 原文地址:https://www.cnblogs.com/plBlog/p/13906288.html
Copyright © 2011-2022 走看看