我想搜索匹配团队名称的数组,然后向上工作以获取整个匹配的数据,但我不确定如何这样做。 JSON数组如下所示:搜索匹配字符串的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);
}
}
}
+0
(results.rounds [matchday] .matches [i] .team1.name == team)matchday分配给@Josh –
+0
Matchday是一个前面定义的var,用于帮助排序数据。在完整的一个(https://raw.githubusercontent.com/opendatajson/football.json/master/2016-17/en.1.json)中,每个球队出现了38次,所以我首先计算出哪个比赛日,然后是比赛日我为我想要的球队检查这个名字。 –