[miniProjects] 42_Live User Filter

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

miniProjects

๋ชฉ๋ก ๋ณด๊ธฐ
42/47
post-thumbnail

42_Live User Filter
๐Ÿ’ป ์ฃผ์ œ : Me API๋ฅผ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ํ•„ํ„ฐ ๋งŒ๋“ค๊ธฐ
๐Ÿ‘๐Ÿป https://randomuser.me/

โœจJS

const result = document.getElementById('result')
const filter = document.getElementById('filter')
const listItems = []

getData()

filter.addEventListener('input', (e) => filterData(e.target.value))

async function getData() {
    const res = await fetch('https://randomuser.me/api?results=50')

    const { results } = await res.json()

    // Clear result
    result.innerHTML = ''

    results.forEach(user => {
        const li = document.createElement('li')

        listItems.push(li)

        li.innerHTML = `
            <img src="${user.picture.large}" alt="${user.name.first}">
            <div class="user-info">
                <h4>${user.name.first} ${user.name.last}</h4>
                <p>${user.location.city}, ${user.location.country}</p>
            </div>
        `

        result.appendChild(li)
    })
}

function filterData(searchTerm) {
    listItems.forEach(item => {
        if(item.innerText.toLowerCase().includes(searchTerm.toLowerCase())) {
            item.classList.remove('hide')
        } else {
            item.classList.add('hide')
        }
    })
}

๐Ÿ˜ถ getData()

  • getData() ํ•จ์ˆ˜๋Š” async ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋™๊ธฐ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

  • fetch() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "RandomUser.me" API๋กœ HTTP ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค. API๋Š” 50๊ฐœ์˜ ๋žœ๋ค ์‚ฌ์šฉ์ž ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    API ์‘๋‹ต์„ ๋ฐ›์€ ํ›„, res.json() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‘๋‹ต ๋ฐ”๋””์˜ JSON ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์‹ฑํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JSON ํŒŒ์‹ฑ์ด ์™„๋ฃŒ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•œ๋‹ค.

  • { results }๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์‹ฑ๋œ JSON ๊ฐ์ฒด์—์„œ results ์†์„ฑ์„ ์ถ”์ถœํ•œ๋‹ค. ์ด ์†์„ฑ์—๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ์ฒด๋“ค์˜ ๋ฐฐ์—ด์ด ๋“ค์–ด ์žˆ๋‹ค.

  • result ๋ณ€์ˆ˜๋ฅผ ๋น„์›Œ์„œ ์ด์ „ ์ปจํ…์ธ ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
    ์ฝ”๋“œ๋Š” results ๋ฐฐ์—ด์˜ ๊ฐ ์‚ฌ์šฉ์ž ๊ฐ์ฒด๋ฅผ forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณตํ•œ๋‹ค.
    ๊ฐ ์‚ฌ์šฉ์ž๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๋ชฉ๋ก ํ•ญ๋ชฉ (<li>) ์š”์†Œ๋ฅผ document.createElement('li')๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.

  • ์ƒ์„ฑ๋œ <li> ์š”์†Œ๋ฅผ listItems ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•œ๋‹ค.

  • <li> ์š”์†Œ ๋‚ด๋ถ€์—๋Š” ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด (๋ฐฑํ‹ฑ)์„ ์‚ฌ์šฉํ•˜์—ฌ HTML ๊ตฌ์กฐ๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ์ด ๊ตฌ์กฐ์—๋Š” ์‚ฌ์šฉ์ž์˜ ํฐ ํ”„๋กœํ•„ ์‚ฌ์ง„์„ ๋ณด์—ฌ์ฃผ๋Š” ์ด๋ฏธ์ง€ ํƒœ๊ทธ์™€ ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„ ๋ฐ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” user-info ํด๋ž˜์Šค๊ฐ€ ์ง€์ •๋œ <div> ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋‹ค.

  • ์ƒ์„ฑ๋œ HTML ๋‚ด์šฉ์ด result ์š”์†Œ์— result.appendChild(li)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ถ”๊ฐ€๋œ๋‹ค.

๐Ÿ˜ถfilterData

  • searchTerm์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š”๋‹ค. ์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ listItems ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ์— ๋Œ€ํ•ด forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณตํ•œ๋‹ค.

  • ๋ฐ˜๋ณต๋ฌธ ๋‚ด๋ถ€์—์„œ item์€ listItems ๋ฐฐ์—ด์˜ ๊ฐ ํ•ญ๋ชฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

  • item.innerText๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ํ•ญ๋ชฉ์˜ ํ…์ŠคํŠธ ์ฝ˜ํ…์ธ ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

  • toLowerCase()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰์–ด์™€ ํ•ญ๋ชฉ์˜ ํ…์ŠคํŠธ๋ฅผ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.(๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ๊ฒ€์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด)

  • includes() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ญ๋ชฉ์˜ ํ…์ŠคํŠธ๊ฐ€ ๊ฒ€์ƒ‰์–ด๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
    ๋งŒ์•ฝ ํฌํ•จ๋œ๋‹ค๋ฉด, ํ•ด๋‹น ํ•ญ๋ชฉ์€ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์— ํ•ด๋‹นํ•˜๋ฏ€๋กœ item.classList.remove('hide')๋ฅผ ํ†ตํ•ด hide ํด๋ž˜์Šค๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

  • ๊ฒ€์ƒ‰์–ด๊ฐ€ ํ•ญ๋ชฉ์˜ ํ…์ŠคํŠธ์— ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด item.classList.add('hide')๋ฅผ ํ†ตํ•ด hide ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ์ด๋กœ ์ธํ•ด ํ•ด๋‹น ํ•ญ๋ชฉ์ด ์ˆจ๊ฒจ์ง€๊ฒŒ ๋œ๋‹ค.

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

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

comment-user-thumbnail
2023๋…„ 8์›” 4์ผ

์ •๋ฆฌ๊ฐ€ ์ž˜ ๋œ ๊ธ€์ด๋„ค์š”. ๋„์›€์ด ๋์Šต๋‹ˆ๋‹ค.

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ