VueBloghyhero6

JS 对数组进行拆解截取,实现俩个select互锁的基本思路

2021-07-26 / 2021-07-26 / 333次浏览

最近在开发中使用到了一些小的方法故此总结:
this.typeArr = [{'sign': 'coin_available', 'name': vTipText.coinBalance}, {'sign': 'contract_available', 'name': vTipText.contractAccount}] // 账户对象

let self = this
    let type = ''
    this.JAccountTo.on('change', function () {
      let arr = self.typeArr.slice() // 浅拷贝
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].sign === $(this).val()) {
          type = arr[i]
          arr.splice(i, 1)
        }
      }
      arr.push(type)
      self.JAccountFrom.html(self.loop(arr))

核心原理是 splice 方法对数组进行切割, 重新对数据进行排序渲染,当然其中使用到了slice() 浅拷贝方法不会污染原始数组。 再次就是判断当前 select 的当前选中项目,splice 进行切割回收,追加到数组的最后一位这个样子