[英]How to access all the matches inside matches array
{
"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
}
]
}
]
}
我想记录所有在match数组中的匹配项。 但是我似乎无法访问它们,因为有对象,数组,更多数组和更多对象相互嵌套。 有点困惑。请帮助解释如何在这种情况下访问元素。 使用哪个循环,在遍历对象的情况下该怎么做等等。 希望我已经很详尽地解释了我的问题。
您可以在这里尝试以下代码:
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.rounds
或round.matches
)来访问对象的特定属性。 然后,您可以在阵列功能( .forEach()
你可以读到这里 )来访问每个阵列中的对象。 然后,您只需访问那些子对象的属性。
希望这可以帮助。
根据Phil的评论,类似这样的事情应该可以帮助您遍历比赛并为每次比赛做一些事情。
obj.rounds[0].matches.forEach(match => {
console.log(match);
})
共享的片段实际上是一个对象。 在该对象内部,有一个名为rounds
的键,它也是对象的数组。
因此,data.rounds将给出一个数组值。
在此数组内有匹配项数组。 但是data.rounds是只有一个对象的数组。 因此, data.rounds[0]
将允许访问其值。 [0]是索引,因为数组中的第一个元素位于0 index&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)
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) } }
我认为您的数据是在JSON中构造的,您将使用json_decode函数将其解码,如下所示:
$myMatches = json_decode($yourJSON);
将JSON转换为对象后,您可以按照以下规则访问任何属性:
例如,假设对象“ $ person”具有以下结构:
{
"name": "manutd",
"lastname": "Manchester United",
"phone": "MUN"
}
要打印名称,您必须执行以下操作:
echo $person->name;
例如,同一个人但现在使用数组语法:
[
"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;
}
您知道了,希望对您有所帮助。 我没有测试代码,也许有轻微的语法错误。
您可以获取变量中的所有匹配项,然后使用任何循环对其进行迭代。 如果只有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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.