【问题标题】:How to access all the matches inside matches array如何访问匹配数组中的所有匹配项
【发布时间】:2017-09-01 03:27:23
【问题描述】:
{
  "name": "English Premier League 2015/16",
  "rounds": [
    {
      "name": "Play-Off um 1 Premierleague-Platz:",
      "matches": [
        {
          "date": "2015-08-08",
          "team1": {
            "key": "manutd",
            "name": "Manchester United",
            "code": "MUN"
          },
          "team2": {
            "key": "tottenham",
            "name": "Tottenham Hotspur",
            "code": "TOT"
          },
          "score1": 1,
          "score2": 0
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "bournemouth",
            "name": "Bournemouth",
            "code": "BOU"
          },
          "team2": {
            "key": "astonvilla",
            "name": "Aston Villa",
            "code": "AVL"
          },
          "score1": 0,
          "score2": 1
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "everton",
            "name": "Everton",
            "code": "EVE"
          },
          "team2": {
            "key": "watford",
            "name": "Watford",
            "code": "WAT"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "leicester",
            "name": "Leicester City",
            "code": "LEI"
          },
          "team2": {
            "key": "sunderland",
            "name": "Sunderland",
            "code": "SUN"
          },
          "score1": 4,
          "score2": 2
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "norwich",
            "name": "Norwich",
            "code": "NOR"
          },
          "team2": {
            "key": "crystalpalace",
            "name": "Crystal Palace",
            "code": "CRY"
          },
          "score1": 1,
          "score2": 3
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "chelsea",
            "name": "Chelsea",
            "code": "CHE"
          },
          "team2": {
            "key": "swansea",
            "name": "Swansea",
            "code": "SWA"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "arsenal",
            "name": "Arsenal",
            "code": "ARS"
          },
          "team2": {
            "key": "westham",
            "name": "West Ham United",
            "code": "WHU"
          },
          "score1": 0,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "newcastle",
            "name": "Newcastle United",
            "code": "NEW"
          },
          "team2": {
            "key": "southampton",
            "name": "Southampton",
            "code": "SOU"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "stoke",
            "name": "Stoke City",
            "code": "STK"
          },
          "team2": {
            "key": "liverpool",
            "name": "Liverpool",
            "code": "LIV"
          },
          "score1": 0,
          "score2": 1
        },
        {
          "date": "2015-08-10",
          "team1": {
            "key": "westbrom",
            "name": "West Bromwich Albion",
            "code": "WBA"
          },
          "team2": {
            "key": "mancity",
            "name": "Manchester City",
            "code": "MCI"
          },
          "score1": 0,
          "score2": 3
        }
      ]
    }
  ]
}

我想记录匹配数组中的所有匹配项。但我似乎无法访问它们,因为有对象、数组、更多数组和更多嵌套在彼此内部的对象。有点困惑。请帮助解释如何在这种情况下访问元素。使用哪个循环,在循环对象的情况下要做什么等等。希望我已经非常详细地解释了我的问题。

【问题讨论】:

  • obj.rounds[0].matches。你在尝试什么?
  • 如果rounds数组中有多个对象(比如50个)怎么办?
  • 您的问题在这种情况下并不反映现实。您想究竟matches 数据做什么?你试过什么?
  • 我正在尝试访问 team1 的日期和密钥 team1 的名称以及类似的团队 2 的所有内容

标签: javascript


【解决方案1】:

你可以在这里试试这个代码:

const data = {"name":"English Premier League 2015/16","rounds":[{"name":"Play-Off um 1 Premierleague-Platz:","matches":[{"date":"2015-08-08","team1":{"key":"manutd","name":"Manchester United","code":"MUN"},"team2":{"key":"tottenham","name":"Tottenham Hotspur","code":"TOT"},"score1":1,"score2":0},{"date":"2015-08-08","team1":{"key":"bournemouth","name":"Bournemouth","code":"BOU"},"team2":{"key":"astonvilla","name":"Aston Villa","code":"AVL"},"score1":0,"score2":1},{"date":"2015-08-08","team1":{"key":"everton","name":"Everton","code":"EVE"},"team2":{"key":"watford","name":"Watford","code":"WAT"},"score1":2,"score2":2},{"date":"2015-08-08","team1":{"key":"leicester","name":"Leicester City","code":"LEI"},"team2":{"key":"sunderland","name":"Sunderland","code":"SUN"},"score1":4,"score2":2},{"date":"2015-08-08","team1":{"key":"norwich","name":"Norwich","code":"NOR"},"team2":{"key":"crystalpalace","name":"Crystal Palace","code":"CRY"},"score1":1,"score2":3},{"date":"2015-08-08","team1":{"key":"chelsea","name":"Chelsea","code":"CHE"},"team2":{"key":"swansea","name":"Swansea","code":"SWA"},"score1":2,"score2":2},{"date":"2015-08-09","team1":{"key":"arsenal","name":"Arsenal","code":"ARS"},"team2":{"key":"westham","name":"West Ham United","code":"WHU"},"score1":0,"score2":2},{"date":"2015-08-09","team1":{"key":"newcastle","name":"Newcastle United","code":"NEW"},"team2":{"key":"southampton","name":"Southampton","code":"SOU"},"score1":2,"score2":2},{"date":"2015-08-09","team1":{"key":"stoke","name":"Stoke City","code":"STK"},"team2":{"key":"liverpool","name":"Liverpool","code":"LIV"},"score1":0,"score2":1},{"date":"2015-08-10","team1":{"key":"westbrom","name":"West Bromwich Albion","code":"WBA"},"team2":{"key":"mancity","name":"Manchester City","code":"MCI"},"score1":0,"score2":3}]}]};

data.rounds.forEach((round) => {
  round.matches.forEach((match) => {
    console.log(`Results ${ match.score1 } | ${ match.score2 }`);
  })
});

基本上,您使用的是数组和对象引用的混合。 您可以使用对象引用(data.roundsround.matches)来获取对象的特定属性。然后你可以数组函数(.forEach(),你可以阅读here)来访问每个数组中的对象。然后您只需访问这些子对象的属性。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    根据 Phil 的评论,类似这样的内容应该可以帮助您遍历匹配项并对每个匹配项做一些事情。

    obj.rounds[0].matches.forEach(match => {
        console.log(match);
    })
    

    【讨论】:

      【解决方案3】:

      sn-p 共享实际上是一个对象。在这个对象内部有一个名为 rounds 的键,它也是对象数组。

      所以 data.rounds 会给出一个数组的值。

      在这个数组里面有一个匹配数组。但是 data.rounds 是只有一个对象的数组。因此data.rounds[0] 将允许访问它的值。 [0] 是索引,因为在数组中第一个元素位于 0 索引处 & data.rounds[0].matches 将给出匹配数组

      var data = {
        "name": "English Premier League 2015/16",
        "rounds": [{
          "name": "Play-Off um 1 Premierleague-Platz:",
          "matches": [
           //other objects
        ]
      }
      console.log(data.rounds[0].matches)
      

      DEMO

      【讨论】:

      • “sn-p 共享实际上是一个对象”
      • 谢谢,但是如果 rounds 数组中有 50 个对象怎么办。那我该怎么办?
      【解决方案4】:

      我请客,您的数据采用 JSON 结构,您将使用 json_decode 函数进行解码,如下所示:

       $myMatches = json_decode($yourJSON);
      

      在将 JSON 转换为对象后,您可以按照以下规则访问任何属性:

      1. JSON 上的每个左大括号都会转换为一个对象,您必须使用如下箭头语法:$myMatches->attribute

      例如,想象一个具有以下结构的对象“$person”:

       {
           "name": "manutd",
           "lastname": "Manchester United",
           "phone": "MUN"
       }
      

      要打印你必须做的名字:

       echo $person->name;
      
      1. 另一方面,每个普通括号都会转换为一个数组,您必须使用括号来访问该信息(与任何其他数组一样),如下所示:$myMatches['attribute']

      例如同一个人,但现在使用数组语法:

       [
           "name": "manutd",
           "lastname": "Manchester United",
           "phone": "MUN"
       ]
      

      要打印你必须做的名字:

      echo $person['name'];
      

      现在,特别针对您的问题

      也就是说,如果你想打印第一场比赛第一轮的日期,你必须这样做:

      echo $myMatches->rounds[0]->matches[0]->date;
      

      我已经为你准备了这个函数:

      //This function returns all the matches in one tournament round 
      function getMatchesOfRound($myMatches, $roundNumber)
      {
          return $myMatches->rounds[$roundNumber]->matches;
      }
      
      //This function returns all the matches played by $teamKey 
      function getAllMatchesOfTeam($myMatches, $teamKey){
          $matches = [];
          foreach($myMatches->rounds as $rounds)
              foreach($rounds->matches as $match)
                  if($match->team1->key == $teamKey || $match->team2->key == $teamKey) $matches[] = $match;
      
          return $matches;
      }
      
      //this function determines the winner of a given match and returns the $teamKey
      
      function getWinerFromMatch($match){
          if($match->score1 > $match->score2) return $match->team1;
          else if($match->score1 < $match->score2) return $match->team2;
          else return null;
      }
      
      //This function returns all matches won by $teamKey
      function getAllMatchesOfTeam($myMatches, $teamKey){
          $matches = [];
          foreach($myMatches->rounds as $rounds)
              foreach($rounds->matches as $match)
                  if(getWinerFromMatch($match) == $teamKey) $matches[] = $match;
          return $matches;
      }
      
      //This function returns all ties
      function getAllMatchesOfTeam($myMatches, $teamKey){
          $matches = [];
          foreach($myMatches->rounds as $rounds)
              foreach($rounds->matches as $match)
                  if(getWinerFromMatch($match) == null) $matches[] = $match;
      
          return $matches;
      }
      

      你明白了,我希望这会有所帮助。我没有测试过代码,可能有一些小的语法错误。

      【讨论】:

        【解决方案5】:

        result = {
        "name": "English Premier League 2015/16",
        "rounds": [
            {
                "name": "Play-Off um 1 Premierleague-Platz:",
                "matches": [
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "manutd",
                            "name": "Manchester United",
                            "code": "MUN"
                        },
                        "team2": {
                            "key": "tottenham",
                            "name": "Tottenham Hotspur",
                            "code": "TOT"
                        },
                        "score1": 1,
                        "score2": 0
            },
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "bournemouth",
                            "name": "Bournemouth",
                            "code": "BOU"
                        },
                        "team2": {
                            "key": "astonvilla",
                            "name": "Aston Villa",
                            "code": "AVL"
                        },
                        "score1": 0,
                        "score2": 1
            },
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "everton",
                            "name": "Everton",
                            "code": "EVE"
                        },
                        "team2": {
                            "key": "watford",
                            "name": "Watford",
                            "code": "WAT"
                        },
                        "score1": 2,
                        "score2": 2
            },
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "leicester",
                            "name": "Leicester City",
                            "code": "LEI"
                        },
                        "team2": {
                            "key": "sunderland",
                            "name": "Sunderland",
                            "code": "SUN"
                        },
                        "score1": 4,
                        "score2": 2
            },
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "norwich",
                            "name": "Norwich",
                            "code": "NOR"
                        },
                        "team2": {
                            "key": "crystalpalace",
                            "name": "Crystal Palace",
                            "code": "CRY"
                        },
                        "score1": 1,
                        "score2": 3
            },
                    {
                        "date": "2015-08-08",
                        "team1": {
                            "key": "chelsea",
                            "name": "Chelsea",
                            "code": "CHE"
                        },
                        "team2": {
                            "key": "swansea",
                            "name": "Swansea",
                            "code": "SWA"
                        },
                        "score1": 2,
                        "score2": 2
            },
                    {
                        "date": "2015-08-09",
                        "team1": {
                            "key": "arsenal",
                            "name": "Arsenal",
                            "code": "ARS"
                        },
                        "team2": {
                            "key": "westham",
                            "name": "West Ham United",
                            "code": "WHU"
                        },
                        "score1": 0,
                        "score2": 2
            },
                    {
                        "date": "2015-08-09",
                        "team1": {
                            "key": "newcastle",
                            "name": "Newcastle United",
                            "code": "NEW"
                        },
                        "team2": {
                            "key": "southampton",
                            "name": "Southampton",
                            "code": "SOU"
                        },
                        "score1": 2,
                        "score2": 2
            },
                    {
                        "date": "2015-08-09",
                        "team1": {
                            "key": "stoke",
                            "name": "Stoke City",
                            "code": "STK"
                        },
                        "team2": {
                            "key": "liverpool",
                            "name": "Liverpool",
                            "code": "LIV"
                        },
                        "score1": 0,
                        "score2": 1
            },
                    {
                        "date": "2015-08-10",
                        "team1": {
                            "key": "westbrom",
                            "name": "West Bromwich Albion",
                            "code": "WBA"
                        },
                        "team2": {
                            "key": "mancity",
                            "name": "Manchester City",
                            "code": "MCI"
                        },
                        "score1": 0,
                        "score2": 3
            }
          ]
        }
          ]
        };
        
        for ( let i=0, totalRounds = result.rounds.length; i < totalRounds; i++) {
          let round = result.rounds[i];
          
          console.log( round.name );
          
          for ( let j=0, totalMatches = round.matches.length; j < totalMatches; j++  ) {
          
            let match = round.matches[j];
            console.log( match.date + ': ' + match.team1.name + " " + match.score1 + " - "  + match.team2.name + " " + match.score2)
          }
        }

        【讨论】:

        • 谢谢,但是如果 rounds 数组中有 50 个对象怎么办。那我该怎么办?
        【解决方案6】:

        您可以获取变量中的所有匹配项,然后使用任何循环对其进行迭代。 如果只有 1 轮,那么这将完成任务。

        var matches = object.rounds[0]. matches;
        for(var match in matches){
            console.log(match);
        }
        

        如果有更多轮次,那么您首先迭代轮次并在该循环内获取该特定轮次中的所有匹配项。

        for(var round in object.rounds){
            var matches = round.matches;
            for(var prop in matches){
                console.log(prop);
            }
        }
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-03
        • 1970-01-01
        • 2020-08-07
        • 2019-01-30
        相关资源
        最近更新 更多