I have a dataset returned by a third party API as JSON.
Now I want to loop through the data and populate a league table at my frontend (using 11 key:values from the array).
I already transformed the object into an array (var standings
) and defined an empty variable "rank". But now I am really stuck on how to proceed, other tutorials just increase my confusion.
Do I need to create eleven empty arrays to grab the required data into them and populate the html using the "new" arrays afterwards? Probably this task can be handled by a "25-line-all-in-super-loop-solution".
This is my Javascript (applause!):
$.ajax({
method: "GET",
async: "True",
dataType: "json",
url: "https://cors-anywhere.herokuapp.com/https://www.api-football.com/demo/api/v2/leagueTable/" + league_id,
success: function(response) {
var standings = response.api.standings;
for (let i = 0; i < standings.length; i++) {
var rank = [];
console.log(standings[i].teamName);
}
The console.log returns undefined
(i tried to print all 20 teamnames within the array).
This is the JSON data (it returns 1 result = 1 league table including all teams in the array with additional data)
{
"api": {
"results": 1,
"standings": [
[
{
"rank": 1,
"team_id": 85,
"teamName": "Paris Saint Germain",
"logo": "https://media.api-football.com/teams/85.png",
"group": "Ligue 1",
"forme": "DLWLL",
"description": "Promotion - Champions League (Group Stage)",
"all": {
"matchsPlayed": 35,
"win": 27,
"draw": 4,
"lose": 4,
"goalsFor": 98,
"goalsAgainst": 31
},
"home": {
"matchsPlayed": 18,
"win": 16,
"draw": 2,
"lose": 0,
"goalsFor": 59,
"goalsAgainst": 10
},
"away": {
"matchsPlayed": 17,
"win": 11,
"draw": 2,
"lose": 4,
"goalsFor": 39,
"goalsAgainst": 21
},
"goalsDiff": 67,
"points": 85,
"lastUpdate": "2019-05-04"
},
{...}
]
]
}
}
And the HTML part to populate (however this would be step 2)
<div class="fifthRow">
<div class="column">
<div class="table" id="rank">
<div><p></p></div>
[...]
<div><p></p></div>
</div>
<div class="table" id="logo">
<div><p>Rank</p></div>
<div><p></p></div>
[...]
<div><p></p></div>
</div>
[...]