如何从以下数据中获得本赛季球队进球的得分。团队名称是函数输入。
我正确地尝试绘制回合并过滤其中team1或team 2 ===输入的球队的比赛,然后减少这些得分的总和吗?
缩短的数据样本:
{
"name": "English Premier League 2014/15",
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2014-08-16",
"team1": {
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
"team2": {
"key": "swansea",
"name": "Swansea",
"code": "SWA"
},
"score1": 1,
"score2": 2
},
{
"date": "2014-08-16",
"team1": {
"key": "leicester",
"name": "Leicester City",
"code": "LEI"
},
"team2": {
"key": "everton",
"name": "Everton",
"code": "EVE"
},
"score1": 2,
"score2": 2
},
到目前为止,我的努力是:
function run (teamName){
function getSum (total, match) {
return total + match.score1;
}
fetch('https://raw.githubusercontent.com/openfootball/football.json/master/2014-15/en.1.json')
.then(response => response.json())
.then(data => console.log(data.rounds.map( matchday => matchday.matches.filter(match => match.team1.name === teamName).reduce(getSum))));
}
run('Liverpool')
答案 0 :(得分:0)
尝试一下
function findScoreByTeamName(strTeamName){
let jsnLeague = {
"name": "English Premier League 2014/15",
"rounds": [
{
"name": "Matchday 1",
"matches": [
{
"date": "2014-08-16",
"team1": {
"key": "manutd",
"name": "Manchester United",
"code": "MUN"
},
"team2": {
"key": "swansea",
"name": "Swansea",
"code": "SWA"
},
"score1": 1,
"score2": 2
},
{
"date": "2014-08-16",
"team1": {
"key": "leicester",
"name": "Leicester City",
"code": "LEI"
},
"team2": {
"key": "everton",
"name": "Everton",
"code": "EVE"
},
"score1": 2,
"score2": 2
}
]
}
]
};
let score = 0;
for(round of jsnLeague.rounds){
for(match of round.matches){
if(match.team1.name == strTeamName){
score += match.score1;
}else if(match.team2.name == strTeamName){
score += match.score2;
}
}
}
console.log(score);
}
//Then call it
findScoreByTeamName("Swansea");
我已经测试了代码here,将其复制粘贴并看一下
希望有帮助!
答案 1 :(得分:0)
这是我的一种功能编程方式的解决方案。您将在函数表达式声明上方直接找到注释:
const JSON_ORIGIN = 'https://raw.githubusercontent.com/openfootball/football.json/master/2014-15/en.1.json'
const download = () =>
fetch(JSON_ORIGIN).then(response => response.json())
const sum = (total, value) => total + value
// get the score comparing first and second team
const resultPerMatch = teamName => match => {
if (match.team1.name === teamName) return match.score1
if (match.team2.name === teamName) return match.score2
// if the chosen team didn't played this game, it gets 0 points
return 0
}
// given a round, sum the scores of this team for each match
const resultsPerRound = teamName => round =>
round.matches.map(resultPerMatch(teamName)).reduce(sum, 0)
// given a team, sum the scores for each round
const resultsPerTeam = (data, teamName) => {
return data.rounds.map(resultsPerRound(teamName)).reduce(sum, 0)
}
// wait until data gets downloaded
download().then(data => {
console.log(resultsPerTeam(data, 'Manchester United')) // 62
console.log(resultsPerTeam(data, 'Liverpool')) // 52
})
答案 2 :(得分:0)
我有一个解决方案,将上述两种解决方案混合使用来检索数据和使用for循环。谢谢您的帮助
const getTeamScore = async (teamName) => {
const response = await fetch('https://raw.githubusercontent.com/openfootball/football.json/master/2014-15/en.1.json');
const json = await response.json();
let score = 0;
for(round of json.rounds){
for(match of round.matches){
if(match.team1.name === teamName){
score += match.score1
}else if(match.team2.name === teamName){
score += match.score2
}else{
score = score
}
}
}
console.log('Total goals scored for the season by ' + teamName + ' were: ' + score )
}
getTeamScore('Manchester United');