【问题标题】:Accessing a Sub Array inside object through a Variable通过变量访问对象内部的子数组
【发布时间】:2021-01-06 15:48:43
【问题描述】:

我从一个如下所示的 API 获取一个数组:

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

我要提取的是具有 2 个子数组(LocalTeam 和访问者,在本例中为 Alianza Local 和 Metapan 访问者)和每个子数组内部的另一个子数组(Substitudes)的 LineUps。

现在我需要将这些信息保存到我的应用程序中的数组中,并且通过第一部分,我能够通过此代码将本地团队和访客团队名称转换为变量,我正在使用带有 Javascript 的 React Native:

const resData = await response.json();

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

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

resData 中得到该结果,但我认为为了获得阵容数组,我可以执行以下操作:

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

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

我面临的问题是,如果我采用第一种方法,API 搜索会寻找一个名为 equipoLocal 的部分,它应该是该变量的团队名称,而第二种方法给出我一个错误。

有什么想法吗?

【问题讨论】:

标签: javascript arrays react-native


【解决方案1】:

您正在寻找的是Square bracket notation,它允许您读取基于字符串的属性。

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

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

【讨论】:

  • 只是想看看我是否正确地理解了你,对不起,如果我问的太多了,但是在不同的数组中获取替代品将是这样的:const subsLocalData = resData.api.fixtures[0].lineups[equipoLocal].substitutes; ???
猜你喜欢
  • 2019-01-26
  • 1970-01-01
  • 2018-11-14
  • 1970-01-01
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-23
相关资源
最近更新 更多