Concept: Filter pattern with fetched data (using for loop), then map to display
Create a FilterPokemonByHeight component that:
https://pokeapi.co/api/v2/pokemon?limit=20.map() in JSX to display the filtered pokemon namesDisplays only pokemon with height > 10:
Pokemon with height > 10:
- venusaur
- charizard
- blastoise
... (only tall pokemon)
import { useState, useEffect } from 'react';
export default function FilterPokemonByHeight() {
const [filteredPokemon, setFilteredPokemon] = useState([]);
useEffect(() => {
async function fetchAndFilterPokemon() {
// Step 1: Fetch list of pokemon
const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
const data = await response.json();
// Step 2: Fetch all pokemon details (same as Kata 15)
const allPokemon = [];
for (let i = 0; i < data.results.length; i++) {
const pokemonResponse = await fetch(data.results[i].url);
const pokemon = await pokemonResponse.json();
allPokemon.push(pokemon);
}
// Step 3: FILTER: Select only pokemon with height > 10 using for loop
const tallPokemon = [];
// Your filter for loop here
setFilteredPokemon(tallPokemon);
}
fetchAndFilterPokemon();
}, []);
if (filteredPokemon.length === 0) {
return <div>Loading...</div>;
}
return (
<div>
<h3>Pokemon with height > 10:</h3>
<ul>
{filteredPokemon.map((pokemon, index) => (
<li key={index}>{pokemon.name}</li>
))}
</ul>
</div>
);
}
for (let i = 0; i < allPokemon.length; i++)if (allPokemon[i].height > 10)tallPokemon.push(allPokemon[i]).map() in JSX to render: filteredPokemon.map((pokemon, index) => ...)import { useState, useEffect } from 'react';
export default function FilterPokemonByHeight() {
const [filteredPokemon, setFilteredPokemon] = useState([]);
useEffect(() => {
async function fetchAndFilterPokemon() {
// Step 1: Fetch list of pokemon
const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
const data = await response.json();
// Step 2: Fetch all pokemon details
const allPokemon = [];
for (let i = 0; i < data.results.length; i++) {
const pokemonResponse = await fetch(data.results[i].url);
const pokemon = await pokemonResponse.json();
allPokemon.push(pokemon);
}
// Step 3: FILTER: Select only pokemon with height > 10 using for loop
const tallPokemon = [];
for (let i = 0; i < allPokemon.length; i++) {
if (allPokemon[i].height > 10) {
tallPokemon.push(allPokemon[i]);
}
}
setFilteredPokemon(tallPokemon);
}
fetchAndFilterPokemon();
}, []);
if (filteredPokemon.length === 0) {
return <div>Loading...</div>;
}
return (
<div>
<h3>Pokemon with height > 10:</h3>
<ul>
{filteredPokemon.map((pokemon, index) => (
<li key={index}>{pokemon.name}</li>
))}
</ul>
</div>
);
}
height > 10)allPokemon array using a for loopif (allPokemon[i].height > 10)tallPokemon.push(allPokemon[i]).map() method: Transform array items into JSX elements for displayfilteredPokemon.map((pokemon, index) => <li>...</li>)pokemon.nameNote: Learn more about:
.filter() method: MDN: Array.filter().map() method: MDN: Array.map()Try filtering by:
height > 10 && weight > 100