I've been working on a simple test project, but i happened to come across this really weird problem. I can't access values in an array.
pokemonStats$: Observable<PokemonStats[]>;
getPokemonStats(id: number): any {
this.pokemonStats$
.pipe(take(1))
.subscribe(stats => {
console.log(stats instanceof Array);
console.log('length', stats.length);
console.log('du', stats);
console.log('test', stats[1]);
});
}
And this is the output:
What's going on here? A colleague suggested it might be an array-like structure, that's why i added the 'instanceof' log. Any help is greatly appreciated.
EDIT: here's the code, where it gets filled
this.pokemonStats$ = this.getAllPokemonStats(ids);
getAllPokemonStats(ids: number[]): Observable<PokemonStats[]> {
const pokemonArray: PokemonStats[] = [];
ids.forEach(id => {
const url = 'https://pokeapi.co/api/v2/pokemon/' + id;
this.http.get(url)
.pipe(take(1))
.subscribe((data: PokemonStatsAPI) => {
pokemonArray.push({
id: data.id,
name: data.name,
speed: data.stats[0].base_stat,
hp: data.stats[5].base_stat,
attack: data.stats[4].base_stat,
defense: data.stats[3].base_stat
});
});
});
return of(pokemonArray);
}