简体   繁体   English

如何在反应中访问多维数组

[英]how to access multidimensional array in react

I am trying to grab out the "teams" array and pull out data such as teamId, etc from it.我正在尝试获取“团队”数组并从中提取诸如 teamId 等数据。 I was wondering if any of you had any tips/help with this.我想知道你们中是否有人对此有任何提示/帮助。 I have used datas.map(data=>()) but it would not allow me to grab from teams, or maybe im doing it wrong and there is another way to map out a multidimensional array?我使用过 datas.map(data=>()) 但它不允许我从团队中抓取,或者我做错了,还有另一种方法来映射多维数组?

{
    "gameId": 3226262256,
    "platformId": "NA1",
    "gameCreation": 1575583791449,
    "gameDuration": 1526,
    "queueId": 400,
    "mapId": 11,
    "seasonId": 13,
    "gameVersion": "9.23.299.3089",
    "gameMode": "CLASSIC",
    "gameType": "MATCHED_GAME",
    "teams": [
        {
            "teamId": 100,
            "win": "Fail",
            "firstBlood": true,
            "firstTower": false,
            "firstInhibitor": false,
            "firstBaron": false,
            "firstDragon": false,
            "firstRiftHerald": false,
            "towerKills": 1,
            "inhibitorKills": 0,
            "baronKills": 0,
            "dragonKills": 2,
            "vilemawKills": 0,
            "riftHeraldKills": 0,
            "dominionVictoryScore": 0,
            "bans": [
                {
                    "championId": 111,
                    "pickTurn": 1
                },
                {
                    "championId": 25,
                    "pickTurn": 2
                },
                {
                    "championId": 24,
                    "pickTurn": 3
                },
                {
                    "championId": 235,
                    "pickTurn": 4
                },
                {
                    "championId": 238,
                    "pickTurn": 5
                }
            ]
        },
        {
            "teamId": 200,
            "win": "Win",
            "firstBlood": false,
            "firstTower": true,
            "firstInhibitor": true,
            "firstBaron": true,
            "firstDragon": true,
            "firstRiftHerald": true,
            "towerKills": 9,
            "inhibitorKills": 2,
            "baronKills": 1,
            "dragonKills": 1,
            "vilemawKills": 0,
            "riftHeraldKills": 1,
            "dominionVictoryScore": 0,
            "bans": [
                {
                    "championId": 266,
                    "pickTurn": 6
                },
                {
                    "championId": 89,
                    "pickTurn": 7
                },
                {
                    "championId": 64,
                    "pickTurn": 8
                },
                {
                    "championId": -1,
                    "pickTurn": 9
                },
                {
                    "championId": 141,
                    "pickTurn": 10
                }
            ]
        }
    ],

Then here is the code I am trying to use it in:然后这里是我试图使用它的代码:

import React from 'react';
import './MatchHistory.css';
import Card from 'react-bootstrap/Card';

const MatchData = ({ datas }) => {


  return (
        <div>    
            <Card>
              <h5 className="card-title">Full Match data test Pull...</h5>
              <h5 className="card-title">Lane:{datas.platformId}</h5> 
              <h5 className="card-title">MapId:{datas.mapId}</h5>
              <h5 className="card-title">Mode: {datas.gameMode}</h5>
              <h5 className="card-title">Duration: {datas.gameDuration}</h5>
              <h5 className="card-title">SeasonId: {datas.seasonId}</h5>
              <h5 className="card-title">Version: {datas.gameVersion}</h5>
              <h5 className="card-title">Type: {datas.gameType}</h5>
              <h5 className="card-title">Team Data: players, bans, etc</h5>
              <h5 className="card-title">Participant info: champ, KDA, etc</h5>
            </Card>  
      </div> 
   )
}

      export default MatchData;

There is generally nothing wrong with your code, and the way you are accessing the data to be rendered on your component.您的代码以及您访问要在组件上呈现的数据的方式通常没有任何问题。 If you would like to iterate through teams and render it, it will not work if you do something like datas.map(data=>(...)) , as this will iterate through the entire datas object.如果您想遍历teams并渲染它,如果您执行类似datas.map(data=>(...))类的datas.map(data=>(...)) ,它将不起作用,因为这将遍历整个datas对象。

Instead, what you should be doing is to do datas.team.map(data => ...) , which will allow you to iterate through the values within team property using Array.map() , which works on arrays.相反,您应该做的是datas.team.map(data => ...) ,这将允许您使用Array.map()遍历team属性中的值,它适用于数组。

import React from 'react';
import './MatchHistory.css';
import Card from 'react-bootstrap/Card';

const MatchData = ({ datas }) => {


  return (
        <div>    
            <Card>
              <h5 className="card-title">Full Match data test Pull...</h5>
              <h5 className="card-title">Lane:{datas.platformId}</h5> 
              <h5 className="card-title">MapId:{datas.mapId}</h5>
              <h5 className="card-title">Mode: {datas.gameMode}</h5>
              <h5 className="card-title">Duration: {datas.gameDuration}</h5>
              <h5 className="card-title">SeasonId: {datas.seasonId}</h5>
              <h5 className="card-title">Version: {datas.gameVersion}</h5>
              <h5 className="card-title">Type: {datas.gameType}</h5>
              <h5 className="card-title">Team Data: {
                datas.team.map(data => {
                  return <div> 
                    // other properties and values within each object in `team`
                  <div>
                })
              }</h5>
              <h5 className="card-title">Participant info: champ, KDA, etc</h5>
            </Card>  
      </div> 
   )
}

export default MatchData;

 const obj = {"gameId":3226262256,"platformId":"NA1","gameCreation":1575583791449,"gameDuration":1526,"queueId":400,"mapId":11,"seasonId":13,"gameVersion":"9.23.299.3089","gameMode":"CLASSIC","gameType":"MATCHED_GAME","teams":[{"teamId":100,"win":"Fail","firstBlood":true,"firstTower":false,"firstInhibitor":false,"firstBaron":false,"firstDragon":false,"firstRiftHerald":false,"towerKills":1,"inhibitorKills":0,"baronKills":0,"dragonKills":2,"vilemawKills":0,"riftHeraldKills":0,"dominionVictoryScore":0,"bans":[{"championId":111,"pickTurn":1},{"championId":25,"pickTurn":2},{"championId":24,"pickTurn":3},{"championId":235,"pickTurn":4},{"championId":238,"pickTurn":5}]},{"teamId":200,"win":"Win","firstBlood":false,"firstTower":true,"firstInhibitor":true,"firstBaron":true,"firstDragon":true,"firstRiftHerald":true,"towerKills":9,"inhibitorKills":2,"baronKills":1,"dragonKills":1,"vilemawKills":0,"riftHeraldKills":1,"dominionVictoryScore":0,"bans":[{"championId":266,"pickTurn":6},{"championId":89,"pickTurn":7},{"championId":64,"pickTurn":8},{"championId":-1,"pickTurn":9},{"championId":141,"pickTurn":10}]}]} Array(obj).forEach(item => { item.teams.forEach(team => { console.log("Team ID: " + team.teamId, team) }) })

You have nested array你有嵌套数组

{
.
.
teams--
      |
      bans--
           | 
}

try this尝试这个

 <h5 className="card-title">
    Team Data: players, bans, etc
    <ul>
      {datas.teams.map((team, i) => {
        return (
          <li key={i}>
            teamId:{team.teamId}//or other fields inside team
            <ul>
              {team.bans.map((ban, i) => {
                return <li key={i}>championId:{ban.championId}</li> //or other fields inside ban
              })}
            </ul>
          </li>
        );
      })}
    </ul>
  </h5>

Working Codesandbox sample工作代码沙盒示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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