Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.4k views
in Technique[技术] by (71.8m points)

两个实现多选的逻辑求优化

    checked(e, index) {
      //e:当前状态false或true
      //index:当前索引

      //方法一:a为数组
      if (e) {
        //选中,就把b数组对应当前索引的数据赋给a数组
        this.a[index] = this.b[index]; 
      } else {
        //取消,就把a对应的数组位置设为空
        this.a[index] = undefined; 
      }
      //...最后整理出a数组里不为undefined的数据返回


      //方法二:a为对象
      if (e) {
        //选中,就把b数组对应当前索引的数据赋给a对象
        this.a[this.b[index].uid] = this.b[index]; 
      } else {
        //取消,直接删除对象属性
        delete this.a[this.b[index].uid]; 
      }
      //把对象数据整理成数组返回
    },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

可以考虑直接挂载到this.b数组

checked(e, index) {
      this.b[index].checked = e;
      return this.b.filter(({ checked }) => checked);
},

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...