【发布时间】:2018-07-19 20:04:32
【问题描述】:
如何在 JavaScript 中的 filter 中应用 filter,同时在数组中查找数组中的值?
比方说,我想获取所有construction 团队中包含Jane Smith 的项目。
我意识到我在这里发明了一些疯狂的 JavaScript,我只是想向你展示我所追求的。
const result = myArray.filter(x => x.type === "construction" && x.team.filter(y => y.name.includes("Jane Smith")));
这是我要过滤的数组:
[
{
"type": "construction",
"name": "Project A",
"team": [
{
"name": "John Doe",
"position": "Engineer"
},
{
"name": "Jane Smith",
"position": "Architect"
}
]
},
{
"type": "construction",
"name": "Project B",
"team": [
{
"name": "Walt Disney",
"position": "Creative Director"
},
{
"name": "Albert Einstein",
"position": "Scientist"
}
]
},
{
"type": "remodelling",
"name": "Project C",
"team": [
{
"name": "John Travolta",
"position": "Manager"
}
]
}
]
【问题讨论】:
标签: javascript