美文网首页
同时满足两个条件的数组过滤

同时满足两个条件的数组过滤

作者: Frank_Fang | 来源:发表于2022-06-30 18:52 被阅读0次

    要过滤掉数组中id是'id01'同时tableId是'tb02'的数组:

    const arr = [
        {
            id: "id01",
            tableId: "tb01",
            name: "表1字段1",
        },
        {
            id: "id02",
            tableId: "tb01",
            name: "表1字段2"
        },
        {
            id: "id01",
            tableId: "tb02",
            name: "表2字段1"
        },
        {
            id: "id01",
            tableId: "tb02",
            name: "表2字段2"
        },
        {
            id: "id02",
            tableId: "tb02",
            name: "表3字段1"
        },
        {
            id: "id01",
            tableId: "tb03",
            name: "表3字段2"
        }
    ]
    console.log(arr)
    
    //filter错误写法
    const filterArr1 = arr.filter(i=>(i.id !== 'id01' && i.tableId !== 'tb02'))
    console.log(filterArr1)
    
    //filter正确写法
    const filterArr2 = arr.filter(i=>!(i.id === 'id01' && i.tableId === 'tb02'))
    console.log(filterArr2)
    

    相关文章

      网友评论

          本文标题:同时满足两个条件的数组过滤

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