简体   繁体   中英

How do I filter a nested array of object in javascript?

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", }, ] }, ] }, ]

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:

在此处输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM