skip to Main Content

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


  1. 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:

    enter image description here

    Login or Signup to reply.
  2. 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)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search