I'm new to JavaScript and currently working on a project using Vue.js, Axios, and the API available at . The goal of this project is to retrieve NBA player statistics for a homework assignment. I could use some assistance in addressing certain issues. Below is the code I have:
var app = new Vue({
el: '#app',
data: {
teams: null,
players: [],
selectedTeam: null,
selectedTeamPlayers: [],
selection: false,
id : [],
season_averages : []
},
methods: {
getAllPlayers() {
axios.get('https://www.balldontlie.io/api/v1/players?per_page=100').then(response => {
let total = response.data.meta.total_pages;
let req = [];
let url = 'https://www.balldontlie.io/api/v1/players?per_page=100&page=';
for (let i = 1; i <= total; i++) {
req.push(axios.get(url + i));
}
axios.all(req).then(axios.spread((...responses) => {
for (let i = 0; i < responses.length; i++) {
let jsonPlayers = responses[i].data.data;
for (let j = 0; j < jsonPlayers.length; j++) {
this.players.push(jsonPlayers[j]);
this.id.push(jsonPlayers[j].id);
}
}
console.log(this.id);
}));
});
},
getSeasons() {
let seasons = getAllplayers();
let sa = [];
for (var i = 0; i < seasons; i++) {
axios.get("https://www.balldontlie.io/api/v1/season_averages?player_ids[]=" + i).then(response => {
sa[i] = response.data.data;
for (var i = 0; i < sa.length; i++) {
this.season_averages.push(sa[i]);
}
});
}
console.log(season_averages);
}
},
mounted() {
this.getSeasons();
this.getAllPlayers();
}
});
In this script, I am requesting NBA player and team data. The first function returns a JSON structure only containing player IDs. The second function aims to return season averages for all players. However, it can only access stats for specific players based on their ID provided as a parameter in the URL.
For example: This URL shows the season averages of the player with an ID of 237.
My objective is to gather data for all players, which requires obtaining all their IDs. That's why I need the first function - to utilize it in the second function to concatenate each ID with the API URL. This way, I can store and access all player stats efficiently.
My query is how to implement a for loop on Axios requests to fetch season averages for each player?
Thank you,
YT