I'm dealing with a specific object and my goal is to filter and retrieve players from both groups based on certain criteria like "show me all players where franchise = Marvel, and power= flight" but I am struggling with the filtering process at multiple levels.
While researching, I came across this answer which didn't quite match my scenario: JavaScript - Filter object based on multiple values
Although I can achieve results by iterating over the groups, there might be numerous groups and I would prefer not to get stuck in that. Is there a better approach to handling this?
oPlayers.players.groups.filter(function(hero){return hero.Id == 1})
oPlayers = {
"players": {
"groups": [
{
"Id": 1,
"hero": [
{
"Id": 1,
"name": "Batman",
"franchise": "DC",
"gender": "Male",
"powers": [{"power":"stealth"},{"power":"intelligence"},{"power":"weaponry"}]
},
{
"Id": 2,
"name": "Ironman",
"franchise": "Marvel",
"gender": "Male",
"powers": [{"power":"flight"},{"power":"intelligence"},{"power":"weaponry"}]
},
{
"Id": 3,
"name": "Supergirl",
"franchise": "DC",
"gender": "Female",
"powers": [{"power":"flight"},{"power":"strength"},{"power":"speed"}]
},
{
"Id": 4,
"name": "Valkyrie",
"franchise": "Marvel",
"gender": "Female",
"powers": [{"power":"flight"},{"power":"intelligence"},{"power":"strength"}]
}
]
},
{
"Id": 2,
"hero": [
{
"Id": 1,
"name": "Batwoman",
"franchise": "DC",
"gender": "Female",
"powers": [{"power":"stealth"},{"power":"intelligence"},{"power":"weaponry"}]
},
{
"Id": 2,
"name": "IronPepper",
"franchise": "Marvel",
"gender": "Female",
"powers": [{"power":"flight"},{"power":"intelligence"},{"power":"weaponry"}]
},
{
"Id": 3,
"name": "Superman",
"franchise": "DC",
"gender": "Male",
"powers": [{"power":"flight"},{"power":"strength"},{"power":"speed"}]
},
{
"Id": 4,
"name": "Thor",
"franchise": "Marvel",
"gender": "Male",
"powers": [{"power":"flight"},{"power":"intelligence"},{"power":"strength"}]
}
]
}
]
}
}