๐Ÿ‘จโ€๐Ÿ’ป ์ˆœ์ˆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์ž๋™ ์™„์„ฑ ์ž…๋ ฅ ์ƒ์ž ๋งŒ๋“ค๊ธฐ ๐Ÿ”ฅ๐Ÿ”ฅ

13131 ๋‹จ์–ด tutorialwebdevbeginnersjavascript
์•ˆ๋…•ํ•˜์„ธ์š” ์—ฌ๋Ÿฌ๋ถ„, ์ž˜ ์ง€๋‚ด๊ณ  ์žˆ๊ณ  ์ด ๊ฒ€์—ญ ๊ธฐ๊ฐ„ ๋™์•ˆ ์ƒˆ๋กœ์šด ๊ฒƒ์„ ๋ฐฐ์šฐ๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‚ด๊ฐ€ ๊ฐ€์ง„ ๋ชจ๋“  ์ž์œ  ์‹œ๊ฐ„์œผ๋กœ ๋ธ”๋กœ๊ทธ๋ฅผ ์“ฐ๊ธฐ ์‹œ์ž‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ธ๋„ ๋ด‰์‡„๋ น ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋™์™„์„ฑ ๊ธฐ๋Šฅ์œผ๋กœ ๊ฐ„๋‹จํ•œ ๊ฒ€์ƒ‰์„ ์ฝ”๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์— ๋Œ€ํ•œ ๋ฐฑ์—”๋“œ๋ฅผ ๋‹ค๋ฃจ์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ ์œ„ํ•ด ๋‹ค๋ฅธ ๋ธ”๋กœ๊ทธ๋ฅผ ์“ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค...

์•„๋ž˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“ค๋ ค๋Š” ๊ฒƒ์˜ ์˜ˆ์ž…๋‹ˆ๋‹ค.



๋จผ์ € ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•œ ์•ฑ์˜ HTML์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Document</title>
   <link rel="stylesheet" href="index.css"/>
</head>
<body>
   <div class="container">
        <div class="search-container">
           <input type="text" onkeyup="filterNames(event)">
       </div>
       <ul class="list-container" id ="list-container">
           <li>Kushal Sharma</li>
       </ul>
   </div>
</body>
<script src="index.js"></script>
</html>

์ด์ œ ์•ฝ๊ฐ„์˜ CSS๋กœ ์•ฑ์˜ ์Šคํƒ€์ผ์„ ์ง€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

*{
   box-sizing: border-box;
   margin: 0px;
   padding: 0px;
}
.container{
   margin-top: 25px;
   margin-left:auto;
   margin-right: auto;
   min-height: 500px;
   width: 80%;
   border: 1px solid black;
}
.search-container{
   width: 100%;
}
.search-container input{
   width: 100%;
   height: 40px;
   font-size: 25px;
}

.list-container li{
   list-style: none;
   border: 1px solid red;
   padding: 15px 15px;
   font-size: 25px;
}


์ด์ œ ์•ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.



์ด ์•„์ฃผ ์ ์€ ์–‘์˜ ์Šคํƒ€์ผ ์ง€์ •์€ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์ž๋™ ์™„์„ฑ ํ”„๋กœ์„ธ์Šค์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅํ•œ ๋ฌธ์ž์™€ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ํ•„ํ„ฐ๋งํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด JSON์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ ˆ์ฝ”๋“œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ index.js๋กœ ์ €์žฅ๋œ ์ƒˆ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

const names = [
   {
       name:"kushal sharma",
   },
   {
       name:"Jeremy",
   },
   {
       name:"john",
   },
   {
       name:"sandeep",
   },
   {
       name:"mohit",
   },
   {
       name:"sanjeev",
   },
   {
       name:"maininder",
   },
   {
       name:"Ajay",
   }
]


(' name = [โ€œkushalโ€,โ€jeremyโ€,โ€johnโ€,โ€sachinโ€] '๊ณผ ๊ฐ™์€ ๊ฐ„๋‹จํ•œ ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด๋„ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ €๋Š” JSON์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค).

index.html ํŒŒ์ผ์„ ์—ด ๋•Œ ์ด๋ฆ„์„ ํ‘œ์‹œํ•˜์—ฌ ์ด๋ฆ„์„ ํ•„ํ„ฐ๋งํ•  ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ ค๋ฉด ๊ฐ ์ด๋ฆ„์„ HTML DOM ์š”์†Œ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฆ„์„ DOM ์š”์†Œ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด UI์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

function renderNames(arrayOfNames) {
   let liElemet = "" ;
   for (let i= 0; i <arrayOfNames.length; i++) {
   liElemet += `<li>${arrayOfNames[i].name}</li>`
   }
   document.getElementById("list-container").innerHTML= liElemet;
}


๊ทธ๋ž˜์„œ ์—ฌ๊ธฐ์„œ ์šฐ๋ฆฌ๋Š” ์ด์ „์— ์ƒ์„ฑํ•œ ์ด๋ฆ„์˜ ๋ฐฐ์—ด๋กœ ๊ตฌ์„ฑ๋œ arrayOfNames ์ธ์ˆ˜๋ฅผ ๋ฐ›์•„๋“ค์ด๋Š” renderNames ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ์ฝ”๋“œ ์ค„์—์„œ ๋ชฉ๋ก์„ ๋งŒ๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์š”์†Œ ์•ˆ์— ์ด๋ฆ„์„ ํ•˜๋‚˜์”ฉ ์ ๋Š”๋‹ค.

    liElemet += `<li>${arrayOfNames[i].name}</li>`
    

    ์ผ๋‹จ ์šฐ๋ฆฌ๋Š” ๋ชจ๋“ 
  • ์ด๋ฆ„์˜ ์š”์†Œ ์šฐ๋ฆฌ๋Š” ์ˆœ์„œ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๋ชฉ๋ก ์š”์†Œ์ธ ๋ชฉ๋ก ์ปจํ…Œ์ด๋„ˆ์˜ ์ž์‹์œผ๋กœ ์ด ๋ชจ๋“  ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ํ•˜๋„๋ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์— ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

    ์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ด๋ฆ„ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜์—ฌ ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    renderNames(names);
    

    ์ด์ œ ๋ชจ๋“  ์ด๋ฆ„ ๋ชฉ๋ก์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.



    ๋‹ค์Œ์œผ๋กœ ์ฃผ์š” ๊ธฐ๋Šฅ์— ๋Œ€ํ•ด ์ž‘์—…ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์ž…๋ ฅํ•˜๋Š” ๋‚ด์šฉ์„ ํŒŒ์•…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด JavaScript ์ฝ”๋“œ์™€ ํ•จ๊ป˜ HTML ์ด๋ฒคํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์—์„œ ๋ณ€๊ฒฝ์ด ๋ฐœ์ƒํ•  ๋•Œ๋งˆ๋‹ค ์ด๋ฒคํŠธ์ž…๋‹ˆ๋‹ค. ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ์ด๋ฒคํŠธ์ž…๋‹ˆ๋‹ค. ๋งˆ์šฐ์Šค๋ฅผ ์ด๋™ํ•˜๊ณ  ์ฐฝ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•˜๋Š” ๊ฒƒ๋„ ์ด๋ฒคํŠธ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ค‘์ด๋ฏ€๋กœ ํ‚ค๋ณด๋“œ ํ‚ค ๋ˆ„๋ฅด๊ธฐ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    <input type="text" onkeyup="filterNames(event)">
    

    ์—ฌ๊ธฐ์— onkeyup ์†์„ฑ์„ ์ถ”๊ฐ€ํ–ˆ์œผ๋ฉฐ filterNames() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์ด๋ฒคํŠธ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ์ธ์ˆ˜์—๋Š” ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ •๋ณด, ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์œ„์น˜, ๋Œ€์ƒ ์š”์†Œ, ๊ฐ’ ๋“ฑ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

    ์ด์ œ filterNames() ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

    function filterNames(event) {
       var searchvalue = event.target.value;
       var filterNames = names.filter((v,i)=>{
           return(v.name.includes(searchvalue));
       })
       renderNames(filterNames);
    }
    

    filterNames() ํ•จ์ˆ˜ ๋‚ด์—์„œ "event.target.value "๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค ํ‚ค๊ฐ€ ๋ˆŒ๋ ธ๋Š”์ง€ ํŒŒ์•…ํ•œ ๋‹ค์Œ ๋ฐฐ์—ด์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ํ•„ํ„ฐ๋งํ•˜๊ณ  ์ž…๋ ฅ๋œ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์ด๋ฆ„๋งŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ filter() ๋ฉ”์„œ๋“œ์—์„œ ๋ฐ˜ํ™˜๋œ ์ƒˆ ์ด๋ฆ„ ๋ฐฐ์—ด๋กœ renderNames() ํ•จ์ˆ˜๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.



    ์ด์ œ ๊ฒ€์ƒ‰ ์˜ˆ์ œ์—์„œ "sa"๋ผ๋Š” ๋ฌธ์ž๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ "sandeep"๊ณผ "sanjeev"๋ผ๋Š” ๋‘ ๊ฐœ์˜ ์ด๋ฆ„์ด ๋ฐ˜ํ™˜๋˜์—ˆ์œผ๋ฉฐ ๋‘˜ ๋‹ค "sa"๋ผ๋Š” ๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๋งŒ์„ธ! ์ˆœ์ˆ˜ JavaScript๋กœ ์ž๋™ ์™„์„ฑ ๊ธฐ๋Šฅ์œผ๋กœ ๊ฒ€์ƒ‰์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค!

    ๋‚˜๋Š” ๋‹น์‹ ์ด ์ด๊ฒƒ์„ ์ข‹์•„ํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์งˆ๋ฌธ์ด ์žˆ์œผ๋ฉด ์˜๊ฒฌ์— ์ ์–ด์ฃผ์‹ญ์‹œ์˜ค. twitter์—์„œ ๋‚˜๋ฅผ ํŒ”๋กœ์šฐ โ‡’ ๋‚ด๊ฐ€ ๋งค์ผ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์„ธ์š”.

    ๊ทธ๋ฆฌ๊ณ  ๋‚ด๊ฐ€ ์ƒˆ ๋ธ”๋กœ๊ทธ๋ฅผ ๊ฒŒ์‹œํ•  ๋•Œ ์•Œ๋ฆผ์„ ๋ฐ›์œผ๋ ค๋ฉด Dev์—์„œ ๋‚˜๋ฅผ ํŒ”๋กœ์šฐํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ