0

I am getting an Array from an API which looks like this:

{
    "api": {
        "results": 1,
        "fixtures": [
            {
                "fixture_id": 647661,
                "league_id": 2979,
                "league": {
                    "name": "Primera Division",
                    "country": "El-Salvador",
                    "logo": "https://media.api-sports.io/football/leagues/370.png",
                    "flag": "https://media.api-sports.io/flags/sv.svg"
                },
                "event_date": "2021-01-03T21:00:00+00:00",
                "event_timestamp": 1609707600,
                "firstHalfStart": 1609707600,
                "secondHalfStart": 1609711200,
                "round": "Apertura - 2nd Phase - 9",
                "status": "Match Finished",
                "statusShort": "FT",
                "elapsed": 90,
                "venue": "Estadio Cuscatlán",
                "referee": "I. Barton",
                "homeTeam": {
                    "team_id": 4299,
                    "team_name": "Alianza",
                    "logo": "https://media.api-sports.io/football/teams/4299.png"
                },
                "awayTeam": {
                    "team_id": 4304,
                    "team_name": "Isidro Metapán",
                    "logo": "https://media.api-sports.io/football/teams/4304.png"
                },
                "goalsHomeTeam": 1,
                "goalsAwayTeam": 0,
                "score": {
                    "halftime": "0-0",
                    "fulltime": "1-0",
                    "extratime": null,
                    "penalty": null
                },
                "events": [
                    {
                        "elapsed": 46,
                        "elapsed_plus": null,
                        "team_id": 4299,
                        "teamName": "Alianza",
                        "player_id": 108658,
                        "player": "C. Flores",
                        "assist_id": 79308,
                        "assist": "F. Ponce",
                        "type": "subst",
                        "detail": "F. Ponce",
                        "comments": null
                    },
                    {
                        "elapsed": 46,
                        "elapsed_plus": null,
                        "team_id": 4299,
                        "teamName": "Alianza",
                        "player_id": 108806,
                        "player": "O. Rodriguez",
                        "assist_id": 108687,
                        "assist": "W. Cienfuegos",
                        "type": "subst",
                        "detail": "W. Cienfuegos",
                        "comments": null
                    },
                    {
                        "elapsed": 64,
                        "elapsed_plus": null,
                        "team_id": 4304,
                        "teamName": "Isidro Metapán",
                        "player_id": 108662,
                        "player": "Ricardinho",
                        "assist_id": 109179,
                        "assist": "J. Machado",
                        "type": "subst",
                        "detail": "J. Machado",
                        "comments": null
                    },
                    {
                        "elapsed": 75,
                        "elapsed_plus": null,
                        "team_id": 4299,
                        "teamName": "Alianza",
                        "player_id": 128285,
                        "player": "M. Mercado",
                        "assist_id": 51047,
                        "assist": "R. Zelaya",
                        "type": "subst",
                        "detail": "R. Zelaya",
                        "comments": null
                    },
                    {
                        "elapsed": 82,
                        "elapsed_plus": null,
                        "team_id": 4304,
                        "teamName": "Isidro Metapán",
                        "player_id": 108668,
                        "player": "H. Sosa",
                        "assist_id": 108727,
                        "assist": "M. Márquez",
                        "type": "subst",
                        "detail": "M. Márquez",
                        "comments": null
                    },
                    {
                        "elapsed": 89,
                        "elapsed_plus": null,
                        "team_id": 4299,
                        "teamName": "Alianza",
                        "player_id": 108687,
                        "player": "W. Cienfuegos",
                        "assist_id": null,
                        "assist": null,
                        "type": "Goal",
                        "detail": "Normal Goal",
                        "comments": null
                    },
                    {
                        "elapsed": 90,
                        "elapsed_plus": 3,
                        "team_id": 4299,
                        "teamName": "Alianza",
                        "player_id": 59170,
                        "player": "O. Blanco",
                        "assist_id": 108851,
                        "assist": "J. Contreras",
                        "type": "subst",
                        "detail": "J. Contreras",
                        "comments": null
                    }
                ],
                "lineups": {
                    "Alianza": {
                        "coach": "M. Meléndez",
                        "coach_id": 5442,
                        "formation": null,
                        "startXI": [
                            {
                                "team_id": 4299,
                                "player_id": 108907,
                                "player": "M. González",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108762,
                                "player": "I. Mancia",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108648,
                                "player": "R. Marroquin",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108763,
                                "player": "R. Clavel",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108647,
                                "player": "J. Portillo",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108682,
                                "player": "J. Jiménez",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108658,
                                "player": "C. Flores",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108806,
                                "player": "O. Rodriguez",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108706,
                                "player": "N. Orellana",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 59170,
                                "player": "O. Blanco",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 128285,
                                "player": "M. Mercado",
                                "number": null,
                                "pos": null
                            }
                        ],
                        "substitutes": [
                            {
                                "team_id": 4299,
                                "player_id": 79308,
                                "player": "F. Ponce",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108687,
                                "player": "W. Cienfuegos",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 51047,
                                "player": "R. Zelaya",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4299,
                                "player_id": 108851,
                                "player": "J. Contreras",
                                "number": null,
                                "pos": null
                            }
                        ]
                    },
                    "Isidro Metapán": {
                        "coach": "J. Figueroa",
                        "coach_id": 13505,
                        "formation": null,
                        "startXI": [
                            {
                                "team_id": 4304,
                                "player_id": 108892,
                                "player": "O. Pleitez",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108852,
                                "player": "M. Molina",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108872,
                                "player": "M. Mejia",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 71241,
                                "player": "J. Barahona",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108668,
                                "player": "H. Sosa",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 92588,
                                "player": "J. Williams",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108781,
                                "player": "R. Batres",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108771,
                                "player": "I. Castro",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108788,
                                "player": "J. Ortíz",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 109033,
                                "player": "B. López",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108662,
                                "player": "Ricardinho",
                                "number": null,
                                "pos": null
                            }
                        ],
                        "substitutes": [
                            {
                                "team_id": 4304,
                                "player_id": 109179,
                                "player": "J. Machado",
                                "number": null,
                                "pos": null
                            },
                            {
                                "team_id": 4304,
                                "player_id": 108727,
                                "player": "M. Márquez",
                                "number": null,
                                "pos": null
                            }
                        ]
                    }
                },
                "statistics": null,
                "players": null
            }
        ]
    }
}

And what I want to extract is the LineUps which have 2 Sub Arrays (LocalTeam and Visitor, in this case Alianza Local and Metapan Visitor) and inside of each sub-array another sub Array (Substitudes).

Now I need to save to Arrays in my App that information, and through the first part I'm able to get the Local Team and Visitor Team Name into Variables through this Code, I'm using React Native with Javascript:

const resData = await response.json();

const equipoLocal = resData.api.fixtures[0].homeTeam.team_name;

const equipoVisitante = resData.api.fixtures[0].awayTeam.team_name;

Having that result inside of resData however, I thought that in order to get the Arrays of lineup I could do something like this:

const lineUpLocalData = resData.api.fixtures[0].lineups.equipoLocal;

or

const lineUpLocalData = resData.api.fixtures[0].lineups.${equipolocal};

The problem that I'm facing is that if I go with the first approach the API search is looking for a section called equipoLocal which should be the Team name of that Variable, and the Second gives me an Error.

Any Ideas?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Rene Alas
  • 481
  • 4
  • 14
  • 1
    Does this answer your question? [Dynamically access object property using variable](https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable) – Heretic Monkey Jan 06 '21 at 15:52

1 Answers1

0

What you're looking for is Square bracket notation which allows you to read a property based on a string.

const resData = await response.json();
const equipoLocal = resData.api.fixtures[0].homeTeam.team_name;

const lineUpLocalData = resData.api.fixtures[0].lineups[equipoLocal];
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Just to see if I get you correctly and Sorry if this is too much of me asking but getting the substitutes in a different array will be something like this: `const subsLocalData = resData.api.fixtures[0].lineups[equipoLocal].substitutes;` ??? – Rene Alas Jan 06 '21 at 15:58