【小程序】改变数组元素位置

// 上移
 resumeUp(e) {
      const index = e.currentTarget.dataset.index // 获取下标
      // 在wxml里定义一下property
      const currentItem = e.currentTarget.dataset.property // 获取当前点击数组元素
      if (index <= 0) { // 如果是0的话,就不让点击了
        return false;
      }
      this.data.resumeList.splice(index - 1, 0, currentItem)
      this.data.resumeList.splice(index + 1, 1)
      this.setData({
        resumeList: this.data.resumeList,
      })
    },
// 下移
  resumeDown(e) {
      const index = e.currentTarget.dataset.index // 获取下标
      // 在wxml里定义一下property
      const currentItem = e.currentTarget.dataset.property // 获取当前点击数组元素
      if (index == this.data.resumeList.length - 1) { // 如果是最后一项,就不让点击了
        return false;
      }
      this.data.resumeList.splice(index + 2, 0, currentItem)
      this.data.resumeList.splice(index, 1)
      this.setData({
        resumeList: this.data.resumeList,
      })
    },
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容