How do I get all football teams from different clubs?
Array structure:
An array of clubs > each club has multiple teams > each team has numerous events (games).
My goal is to filter all team objects from each club with the sport attribute == "football".
[{
_id: "afsajjenfjnjngiessnagkl",
logo: "picture.jpeg",
name: "Club1",
teams:[
{
_id: "akjjkngho3nkjk232kl2ml24",
sport: "football",
events: [
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Football Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
{
_id: "akjjktfsho3gag232kl2ml24",
sport: "Wrestling",
events: [
{
_id: "nafhjnkns3jn4s2nkjn2b4kjkn",
opponent: "Other Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
]
},
{
_id: "afsajjenfjnjngienagkl",
logo: "picture.jpeg",
name: "Club2",
teams:[
{
_id: "akjjkngho3nkjk232kl2ml24",
sport: "football",
events: [
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Football Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
{
_id: "akjjktfsho3gag232kl2ml24",
sport: "Wrestling",
events: [
{
_id: "nafhjnkns3jn4s2nkjn2b4kjkn",
opponent: "Other Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
]
},
]
desired outcome:
filter only football teams from different Clubs.
[{
_id: "afsajjenfjnjngiengaagkl",
logo: "picture.jpeg",
name: "Club1",
teams:[
{
_id: "akjjkngho3nkjk232kl2ml24",
sport: "football",
events: [
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Football Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
]
},
{
_id: "afsajjenfjnjngienagkl",
logo: "picture.jpeg",
name: "Club2",
teams:[
{
_id: "akjjkngho3nkjk232kl2ml24",
sport: "football",
events: [
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Football Club",
},
{
_id: "nafkjnkn23jn4j2nkjn2b4kjkn",
opponent: "Other Club",
},
]
},
]
},
]
2
Answers
One way to do this is in this way ->
})
You basically have to map the initial array in order to get the individual objects inside of it.
Then you simply have to map the "teams" property which is also an array and it contains the "sport" value which you are looking for and from there on it’s a simple validation to check whether the team matches "football".
Output:
you do like this to filter on behalf of football.