Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JSON数组中搜索匹配字符串,然后向上搜索?_Javascript_Jquery_Arrays_Json - Fatal编程技术网

Javascript 在JSON数组中搜索匹配字符串,然后向上搜索?

Javascript 在JSON数组中搜索匹配字符串,然后向上搜索?,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我试图在数组中搜索匹配的球队名称,然后向上搜索以获取整个比赛的数据,但是我不确定如何执行此操作。JSON数组如下所示: { "name": "English Premier League 2016/17", "rounds": [ { "name": "Matchday 1", "matches": [ { "date": "2017-05-21", "team1": { "k

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

{
  "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
        }
      ]
    }
  ]
}
目前,我的代码如下所示,但它表示无法读取未定义的属性“team1”

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(results.rounds[matchday].matches[i].team1.name == team)
          {
            console.log(results.rounds[matchday].matches.team1.name);
          }
        }
    }
var团队=斯旺西;
$.getJSON(“https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json“,函数(结果){
对于(变量i=0;iif(results.rounds[matchday].matches[i].team1.name==team)
{

console.log(results.rounds[matchday].matches.team1.name); } } }
您可以执行以下操作:

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);
             }
          }
        }
    }
var团队=斯旺西;
$.getJSON(“https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json“,函数(结果){
对于(变量i=0;i
我只是在查找团队名称时添加了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.');
                         }
                    }
                })
           })
}
var团队=斯旺西;
$.getJSON(“https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json“,函数(结果){
对于(变量i=0;i对于(var j=0;jif(results.rounds[matchday].matches[j].team1.name==team){
console.log(results.rounds[i].matches[j].team1.name);
console.log(results.rounds[i]。匹配[j]。日期);
}
}
}
}
由于已将matchday指定给某个特定变量,因此不希望进入并循环所有匹配。
您可以检查迭代器i和matchday是否相等,然后查找团队,以获得包含您团队的所有匹配的列表:

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);
})

(我将
team
改为曼联,假设“斯旺西”是一个意外错误。)

(results.rounds[matchday].matches[I].team1.name==team)matchday分配给@JoshMatchday的是先前定义的一个变量,用于帮助对数据进行排序。完整的()每个队出场38次,所以我首先计算出哪一个比赛日,然后在比赛日内检查我想要的队的名称。获取“未捕获的TypeError:results.rounds[matchday]。find不是一个函数”关于这段代码?编辑:请忽略这一点,这是我在测试时意外留下的另一个损坏的解决方案;该解决方案有效!然后我如何使用它从该条目中返回并收集日期、team1、team2、score1和score2?@JoshCawthorne因为您已经在匹配的团队对象中,您可以通过匹配来获取日期[j].date@JoshCawthorne同样,你可以通过匹配获得所有其他值[j]。(任意键)非常感谢!@JoshCawthorne是我的答案很有帮助或解决了你的问题谢谢你的答案。但是作为利兹球迷,我可以向你保证这不是一个bug。
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);
})