美文网首页
全选:数组filter、some、foreach例子

全选:数组filter、some、foreach例子

作者: Do_Du | 来源:发表于2019-12-17 13:39 被阅读0次

1、列表全选:当其中有一个没有选中时全选取消
数组filter用法温习:filter过滤满足条件的数组,并返回满足的条件的数组
法1:

    var n = this.list.filter(item=> {
      return item.check == true;
    }).length;

  n == this.list.length ? this.checkAll = true : this.checkAll = false;

法2:

    this.checkAll = this.list.every(item=> { // 每个条件都满足就返回true 只要有一个不满足就返回false
      return item.check
    })

利用filter代码简写 删除满足条件的一个数组元素

 var arr = [{ name: 'Lily', age: '01' }, { name: 'Lucy', age: '02' }]
    // arr.forEach((item, index) => {
    //   if (item.name === 'Lily') {
    //     arr.splice(index, 1)
    //   }
    // })
    // console.log(arr)
    arr = arr.filter(item => item.name !== 'Lily')

2、全选按钮 当点击全选时所有列的状态和它一样,即用foreach改变每列的选中状态等于全选状态

checkAll: false, // 全选状态

this.list.forEach(item=>{
  item.check = this.checkAll
})

相关文章

网友评论

      本文标题:全选:数组filter、some、foreach例子

      本文链接:https://www.haomeiwen.com/subject/dhusnctx.html