How do I apply filter
within a filter
in JavaScript while looking for values in an array within the array?
Say, I want to get all construction
projects whose teams include Jane Smith
.
I realize I'm inventing some crazy JavaScript here, I'm just trying to show you what I'm after.
const result = myArray.filter(x => x.type === "construction" && x.team.filter(y => y.name.includes("Jane Smith")));
Here's the array I want to filter:
[
{
"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"
}
]
}
]