I am working on a PokeDex project using the PokeAPI, and could use your help. While trying to handle the json i receive from the API, I am faced with two seemingly distinct array types:
Console representation of arrays
For example, I am able to retrieve the names and urls of the first type of array, but unable and/or unsure of how to for example retrieve the type value for bulbasaur.
I think it has something to do with the different way I am filling the arrays, but not sure.
Here is my code:
class App extends Component {
constructor() {
super();
this.state = {
pokemonArray: [],
pokemonInfoArray: [],
searchfield: '',
}
}
componentDidMount() {
this.fetchKantoDex()
}
// Fetches the pokemon that reside in Kanto.
fetchKantoDex = () => {
fetch('https://pokeapi.co/api/v2/pokemon?limit=10')
.then(response => response.json())
.then(data => this.setState({ pokemonArray: data.results}))
.then(this.fetchSinglePokemon)
}
// Is called by other fetch methods. Loops through and fetches the information pertaining to
// each pokeon fetched, and stores their info in a seperate array.
fetchSinglePokemon = () => {
let tempInfo = [];
for(let i = 0; i < this.state.pokemonArray.length;i++){
let url = this.state.pokemonArray[i].url;
fetch(url)
.then(response => response.json())
.then(pokedata => tempInfo.push(pokedata))
.then(pokedata => console.log(pokedata.results))
}
this.setState({ pokemonInfoArray: tempInfo})
// console.log(this.state.pokemonArray)
console.log(this.state.pokemonInfoArray)
}