[miniProjects] 37_Pokedex

๋ณด๋ฆฌยท2023๋…„ 6์›” 28์ผ
0

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
37/47

37_Pokedex

๐Ÿ’ป์ฃผ์ œ : ํฌํ‚ค API๋ฅผ ์‚ฌ์šฉํ•ด ํฌ์ผ“๋ชฌ ๋„๊ฐ ๋งŒ๋“ค๊ธฐ

โœจJS

150์ข…์˜ ํฌ์ผ“๋ชฌ ๋‚˜์˜ค๊ฒŒ

const fetchPokemons = async () => {
    for(let i = 1; i <= pokemon_count; i++) {
        await getPokemon(i)
    }
}

์ด 150๊ฐœ์˜ ํฌ์ผ“๋ชฌ์ด ๋‚˜์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— for ๋ฐ˜๋ณต๋ฌธ์„ ์จ 150๊ฐœ๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ•จ

ํฌ์ผ“๋ชฌ api๊ฐ€์ ธ์˜ค๊ธฐ

const getPokemon = async (id) => {
    const url = `https://pokeapi.co/api/v2/pokemon/${id}`
    const res = await fetch(url)
    const data = await res.json()
    createPokemonCard(data)
}

const data = await res.json()
res๋ผ๋Š” Response ๊ฐ์ฒด์—์„œ JSON ํ˜•์‹์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

fetch ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๋ฉด, fetch ํ•จ์ˆ˜๋Š” Promise ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด Promise ๊ฐ์ฒด๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ ์„œ๋ฒ„์˜ ์‘๋‹ต์„ ๋ฐ›์œผ๋ฉด ์‹คํ–‰๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ์‘๋‹ต ๊ฐ์ฒด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค.

res.json()์€ Response ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ, ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋„ Promise๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ๊ธฐ๋‹ค๋ฆฐ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€ํ™˜๋œ JSON ๋ฐ์ดํ„ฐ๋Š” data ๋ณ€์ˆ˜์— ํ• ๋‹น๋œ๋‹ค.

๐Ÿ‘๐Ÿป์„œ๋ฒ„์˜ ์‘๋‹ต์„ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ data ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ์ดํ›„ ์ฝ”๋“œ์—์„œ๋Š” data ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํฌ์ผ“๋ชฌ ์นด๋“œ ์ƒ์„ฑ

const main_types = Object.keys(colors)

const createPokemonCard = (pokemon) => {
    const pokemonEl = document.createElement('div')
    pokemonEl.classList.add('pokemon')

    //์ฒซ๋ฒˆ์งธ ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ + ๋‚˜๋จธ์ง€ ๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ
    const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1)
    const id = pokemon.id.toString().padStart(3, '0')

    const poke_types = pokemon.types.map(type => type.type.name)
    const type = main_types.find(type => poke_types.indexOf(type) > -1)
    const color = colors[type]

    pokemonEl.style.backgroundColor = color

    const pokemonInnerHTML = `
    <div class="img-container">
        <img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png"" alt="${name}">
    </div>
    <div class="info">
        <span class="number">#${id}</span>
        <h3 class="name">${name}</h3>
        <small class="type">Type: <span>${type}</span> </small>
    </div>
    `

    pokemonEl.innerHTML = pokemonInnerHTML

    poke_container.appendChild(pokemonEl)
}
  • const main_types = Object.keys(colors)
    colors ๊ฐ์ฒด์˜ ํ‚ค๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ main_types ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

Object.keys()๋Š” JavaScript์—์„œ ๋‚ด์žฅ๋œ ๋ฉ”์„œ๋“œ๋กœ, ๊ฐ์ฒด์˜ ์†์„ฑ(ํ”„๋กœํผํ‹ฐ) ์ด๋ฆ„๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” colors ๊ฐ์ฒด์˜ ์†์„ฑ ์ด๋ฆ„๋“ค, ์ฆ‰ ํƒ€์ž…(์˜ˆ: fire, grass, electric ๋“ฑ)๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ main_types ๋ณ€์ˆ˜์— ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด์€ main_types ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜๋ฏ€๋กœ, ์ดํ›„ ์ฝ”๋“œ์—์„œ main_types ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ํƒ€์ž…์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘๐Ÿป์ด ๋ฐฐ์—ด์€ ์ฃผ๋กœ ํฌ์ผ“๋ชฌ์˜ ํƒ€์ž…์„ ์ •์˜ํ•˜๊ณ , ํƒ€์ž…์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์Šคํƒ€์ผ์„ ์ ์šฉํ•œ๋‹ค.

- const id = pokemon.id.toString().padStart(3, '0')
ํฌ์ผ“๋ชฌ์˜ ID๋ฅผ 3์ž๋ฆฌ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

toString() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , padStart(3, '0') ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ 3์ž๋ฆฌ๋กœ ๋งŒ๋“ค๊ณ , ๋ถ€์กฑํ•œ ์ž๋ฆฌ์— '0'์„ ์ฑ„์šด๋‹ค.

๐Ÿ‘๐Ÿป์˜ˆ๋ฅผ ๋“ค์–ด, pokemon.id ๊ฐ’์ด 7์ด๋ผ๋ฉด, id ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜๋Š” ๊ฐ’์€ '007'์ด ๋œ๋‹ค.

  • ํฌ์ผ“๋ชฌ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์ƒ‰์ƒ ๋„ฃ๊ธฐ
	const poke_types = pokemon.types.map(type => type.type.name)
    const type = main_types.find(type => poke_types.indexOf(type) > -1)
    const color = colors[type]

pokemon.types ๋ฐฐ์—ด์—์„œ ๊ฐ ํƒ€์ž…์˜ ์ด๋ฆ„์„ ์ถ”์ถœํ•˜์—ฌ poke_types ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
๐Ÿ‘๐Ÿป์˜ˆ๋ฅผ ๋“ค์–ด, ํฌ์ผ“๋ชฌ์ด ๋ถˆ๊ณผ ํ’€ ํƒ€์ž…์„ ๊ฐ–๋Š” ๊ฒฝ์šฐ poke_types๋Š” ['fire', 'grass']๊ฐ€ ๋  ๊ฒƒ์ด๋‹ค.

main_types ๋ฐฐ์—ด์—์„œ poke_types์— ํฌํ•จ๋œ ์ฒซ ๋ฒˆ์งธ ํƒ€์ž…์„ ์ฐพ๋Š”๋‹ค.
main_types ๋ฐฐ์—ด์€ ์‚ฌ์ „์— ์ •์˜๋œ ์ฃผ์š” ํƒ€์ž…์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ํฌ์ผ“๋ชฌ์˜ ์ฃผ์š” ํƒ€์ž…์„ ๊ฒฐ์ •ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, main_types ๋ฐฐ์—ด์ด ['fire', 'water', 'grass']๋ผ๊ณ  ๊ฐ€์ •ํ•˜๋ฉด, poke_types ๋ฐฐ์—ด์—์„œ ์ฒซ ๋ฒˆ์งธ๋กœ ๋งค์นญ๋˜๋Š” ํƒ€์ž…์„ ์ฐพ์•„์„œ type ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

poke_types.indexOf(type) > -1๋Š” poke_types ๋ฐฐ์—ด์—์„œ type ๊ฐ’์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ๊ฐ’์ด -1๋ณด๋‹ค ํฌ๋‹ค๋Š” ๊ฒƒ์€ type ๊ฐ’์ด poke_types ๋ฐฐ์—ด์— ์กด์žฌํ•œ๋‹ค๋Š” ์˜๋ฏธ๋‹ค.

find() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜

colors ๊ฐ์ฒด์—์„œ type ๋ณ€์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ƒ์„ ์ฐพ์•„ color ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ ํฌ์ผ“๋ชฌ์˜ ํƒ€์ž…์— ๋”ฐ๋ผ ์ ์ ˆํ•œ ์ƒ‰์ƒ์„ ์ง€์ •ํ•˜์—ฌ ํฌ์ผ“๋ชฌ ์นด๋“œ์˜ ๋ฐฐ๊ฒฝ์ƒ‰์„ ์„ค์ •ํ•œ๋‹ค.

profile
์ •์‹ ์ฐจ๋ ค ์ด ๊ฐ๋ฐ•ํ•œ ์„ธ์ƒ์†์—์„œ

0๊ฐœ์˜ ๋Œ“๊ธ€