通过一些具有多个条件的过滤数组
Get filtered array by some with multiple conditions
这是我的数组结构:-
projects
数组:-
let projects = [
{
id: 1,
name: 'Project 1',
techs: [
id: 1
name: 'Tech 1'
]
},
{
id: 2,
name: 'Project 2',
techs: [
id: 1
name: 'Tech 2'
]
},
{
id: 3,
name: 'Project 3',
techs: [
id: 1
name: 'Tech 3'
]
},
{
id: 4,
name: 'Project 4',
techs: [
id: 1
name: 'Tech 1'
]
}
]
现在我可以得到 只有 Tech 1 的筛选项目,如下所示:-
let filteredProjs = projects.filter(proj => proj.techs.some(tech => tech.name === 'Tech 1'))
我想获取 技术名称 = 'Tech 1' & 'Tech 2' 的 已过滤 个项目。但是当我如下所示时,它 return
我什么也没做:-
let filteredProjs = projects.filter(proj => proj.techs.some(tech => tech.name === 'Tech 1' && tech.name === 'Tech 2'))
我该怎么做?
您可能需要尝试使用 OR 运算符 (||
) 而不是 AND (&&
)。
在计算机编程中,AND
returns true
只有当双方的条件都计算为 true
时。
在 OR
的情况下,如果任一方的条件 returns true
.
,则计算结果为 true
在您的情况下,我假设您想过滤掉技术名称等于 'Tech 1' OR
'Tech 2'.
的项目
这是我的数组结构:-
projects
数组:-
let projects = [
{
id: 1,
name: 'Project 1',
techs: [
id: 1
name: 'Tech 1'
]
},
{
id: 2,
name: 'Project 2',
techs: [
id: 1
name: 'Tech 2'
]
},
{
id: 3,
name: 'Project 3',
techs: [
id: 1
name: 'Tech 3'
]
},
{
id: 4,
name: 'Project 4',
techs: [
id: 1
name: 'Tech 1'
]
}
]
现在我可以得到 只有 Tech 1 的筛选项目,如下所示:-
let filteredProjs = projects.filter(proj => proj.techs.some(tech => tech.name === 'Tech 1'))
我想获取 技术名称 = 'Tech 1' & 'Tech 2' 的 已过滤 个项目。但是当我如下所示时,它 return
我什么也没做:-
let filteredProjs = projects.filter(proj => proj.techs.some(tech => tech.name === 'Tech 1' && tech.name === 'Tech 2'))
我该怎么做?
您可能需要尝试使用 OR 运算符 (||
) 而不是 AND (&&
)。
在计算机编程中,AND
returns true
只有当双方的条件都计算为 true
时。
在 OR
的情况下,如果任一方的条件 returns true
.
true
在您的情况下,我假设您想过滤掉技术名称等于 'Tech 1' OR
'Tech 2'.