在数组中查找数组中的值时,如何在 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"
}
]
}
]