搜索JSON数组以匹配字符串,然后向上工作?

时间:2017-07-02 18:25:19

标签: javascript jquery arrays json

我正在尝试在数组中搜索匹配的团队名称,然后向上工作以获取整个匹配的数据,但我不确定如何执行此操作。 JSON数组如下所示:

func * (left: SCNMatrix4, right: SCNMatrix4) -> SCNMatrix4 {
    return SCNMatrix4Mult(left, right)
}

func + (left: SCNVector3, right: SCNVector3) -> SCNVector3 {
    return SCNVector3Make(left.x + right.x, left.y + right.y, left.z + right.z)
}

目前,我的代码如下,但它说它无法读取未定义的属性'team1'。

{
  "name": "English Premier League 2016/17",
  "rounds": [
    {
      "name": "Matchday 1",
      "matches": [
        {
          "date": "2017-05-21",
          "team1": {
            "key": "swansea",
            "name": "Swansea",
            "code": "SWA"
          },
          "team2": {
            "key": "westbrom",
            "name": "West Bromwich Albion",
            "code": "WBA"
          },
          "score1": 1,
          "score2": 2
          },
          {
          "date": "2017-05-21",
          "team1": {
            "key": "watford",
            "name": "Watford",
            "code": "WAT"
          },
          "team2": {
            "key": "mancity",
            "name": "Manchester City",
            "code": "MCI"
          },
          "score1": 2,
          "score2": 1
        }
      ]
    }
  ]
}

4 个答案:

答案 0 :(得分:0)

你可以这样做:

var team = Swansea;

$.getJSON("https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json", function(results) { 
       for(var i = 0; i < results.rounds.length; i++)
        {
          for (var j=0; j< results.rounds[i].matches.length; j++){
             if(results.rounds[i].matches[j].team1.name == team)
             {
                console.log(results.rounds[i].matches[j].team1.name);
             } 
             if (results.rounds[i].matches[j].team2.name == team){
                console.log(results.rounds[i].matches[j].team2.name);
             }
          }
        }
    }

答案 1 :(得分:0)

我只是在找到团队名称时添加console.log()。你可以把你的逻辑放在那里。

var team = Swansea; 

$.getJSON("https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json", function(results) { 
           results.rounds.forEach(round=>{
                Object.keys(round).forEach(key=>{
                    if(key.contains('team')){
                         if (round[key].name === team){
                              console.log('Team identified.');
                         }
                    }
                })
           })
}

答案 2 :(得分:0)

var team = Swansea;

$.getJSON("https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json", function(results) { 
       for(var i = 0; i < results.rounds.length; i++){
            if(i == matchday){
                for (var j=0; j< results.rounds[matchday].matches.length; j++){
                     if(results.rounds[matchday].matches[j].team1.name == team){
                        console.log(results.rounds[i].matches[j].team1.name);
console.log(results.rounds[i].matches[j].date);
                     }
                  }
            }

        }

由于您将比赛日分配给某个特定变量,因此您不想进入并循环遍历所有匹配项。 您可以检查迭代器i和matchday是否相等,然后查找团队

答案 3 :(得分:0)

获取包含您的团队的所有比赛的列表:

<!-- jQuery 3.2.1 -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Bootstrap JS -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

(我将var team = 'Manchester United'; $.getJSON("https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json", (results) => { let matches = []; results.rounds.forEach(round => { matches = matches.concat(round.matches.filter(match => { return (match.team1.name === team || (match.team2.name === team)) })); }); console.log(matches); }) 改为曼联,假设'斯旺西'是一个意想不到的错误。)