Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k 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 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...