I am fetching data from an API which returns an array of objects in this format.
{Name: 'hitmonchan', Description: 'Fighting-type Pokémon introduced in Generation I'}
{Name: 'Gogoat', Description: 'A quadrupedal, hooved Pokémon similar to a goat'}... and so on
Here is what I am attempting to do. I fetch all the pokemons which is over 500, stuff them in an array called allPokemons. Then I want to do stuff with 4 random pokemons from the list. So I will have an array called smallRandomPokemonArray with only four pokemons chosen at random from the allPokemon array.
import { useEffect, useState } from 'react';
import { getallPokemons } from '../services/pokemonapi';
const empty_pokemon = {
'Name': '',
'Description': ''
}
function PokemonComponent() {
const [allPokemons, setAllPokemons] = useState([]);
const [smallRandomPokemonArray, setSmallRandomPokemonArray] = useState([]);
useEffect(() => {
getAll();
}, []);
const getAll = () => {
getallPokemons()
.then(data => {
//actual data with over 500 objects show in log
console.log(data);
setAllPokemons(data);
randomChooser(data.length)
})
}
const randomChooser = (length) => {
let temporaryArray = [];
for (let i = 0; i < 4; i++) {
const randomIndex = Math.floor(Math.random() * length);
//First Problem: this is returning index in numbers followed by undefined... why?
console.log(randomIndex + " " + allPokemons[randomIndex]);
temporaryArray.push(allPokemons[randomIndex]);
//Second Problem: this is also returning "temp undefined"
console.log("temp " + temporaryArray[i]);
}
setSmallRandomPokemonArray(temporaryArray);
}
return (
<>
<div className="pokemondiv">
{
smallRandomPokemonArray.map((element) => (
<div>
<p>{element.Name}</p>
<p>{element.Description}</p>
</div>
))
}
</div>
</>
)
}
export default PokemonComponent;
When I try to print values from the setSmallRandomPokemonArray I get that error:
Uncaught TypeError: Cannot read properties of undefined (reading 'Name')
Also, please look at First problem and Second problem part in the code. They are also appearing as undefined in the console.log. From my understanding, it should work because allPokemons already exist when I start to randomize it. Then I just push four random values to the smallRandomPokemonArray. I dont know why its throwing the error.