要过滤掉数组中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)
网友评论