Is it possible to retrieve a team's total goals scored for the season from the provided data by using the team's name as the input for a function?
Would it be accurate to attempt mapping over the rounds and filtering matches where either team1 or team2 is equal to the inputted team, then reducing the sum of those scores?
Here is a condensed sample of the data:
{
"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
},
This is my current approach:
function calculateGoals (teamName){
function sumScores (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( round => round.matches.filter(match => match.team1.name === teamName).reduce(sumScores))));
}
calculateGoals('Liverpool')