两个数组的过滤

    let a1 = [{rmName:'王五'},{rmName:'李四'},{rmName:'张三'},{rmName:'赵六'}];
    let b2 = [{name:'王五'},{name:'李四'}];
    // 结果:得到a1中除去b2中值的其他数据
    // 方法一:forEach
    let filterA1 = a1.filter(item=>{
        let flag = b2.findIndex(item_=>item_.name==item.rmName);
        return flag==-1?true:false
    }) 
    console.log(filterA1) //[{"rmName": "张三"},{"rmName": "赵六"}]



// 方法二:every检测数组中每个元素是否 都满足所给条件,不满足就返回false let filterA1 = a1.filter(item=>{ return b2.every(item_=>{ return item_.name!=item.rmName }) }) console.log(filterA1) //[{"rmName": "张三"},{"rmName": "赵六"}]

 

posted @ 2023-12-20 11:03  奔跑的哈密瓜  阅读(57)  评论(0编辑  收藏  举报