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",
},
]
},
]
},
]
CodePudding user response:
One way to do this is in this way ->
arr.map((club) => {
club.teams.map((team) => {
if (team.sport == 'football') {
console.log(team) // Your logic here...
}
})
})
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:
CodePudding user response:
you do like this to filter on behalf of football.
const arr = [
{
_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",
},
]
},
]
},
]
let footbal = [];
arr.forEach((a) => {
a.teams.forEach((b) => {
if (b.sport === 'football') {
console.log('true')
footbal.push(b)
} else {
console.log('false')
}
})
/* footbal = a.teams.filter((c) => c.sport === 'football') */
})
console.log(footbal)