[JS] ๐๏ธJavaScript 30 - Day 6

๐Day 6 - Ajax Type Ahead
JavaScript 30์ Day 6 ํ๋ก์ ํธ๋ Fetch API์ ์ ๊ทํํ์์ ์ด์ฉํด ๊ฒ์์ฐฝ์ ๋ฌธ์๋ฅผ ์ ๋ ฅํ๋ฉด ํด๋น ๋ฌธ์๊ฐ ํฌํจ๋ ๋์๋ฅผ ๋์ดํ๋ ํ๋ก์ ํธ์ด๋ค.
๐ค๐์ฝ๋ ๋ชจ์๋ณด๊ธฐ
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Type Ahead ๐</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<form class="search-form">
<input type="text" class="search" placeholder="City or State" />
<ul class="suggestions">
<li>Filter for a city</li>
<li>or a state</li>
</ul>
</form>
<script src="main.js"></script>
</body>
</html>CSS
html {
box-sizing: border-box;
background: linear-gradient(
90deg,
#98ddca 0%,
#d5ecc2 33%,
#ffd3b4 66%,
#ffaaa7 100%
);
font-family: "helvetica neue";
font-size: 20px;
font-weight: 200;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
input {
width: 100%;
padding: 20px;
}
.search-form {
max-width: 400px;
margin: 50px auto;
}
input.search {
margin: 0;
text-align: center;
outline: 0;
border: 10px solid #f7f7f7;
width: 120%;
left: -10%;
position: relative;
top: 10px;
z-index: 2;
border-radius: 5px;
font-size: 40px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.19);
}
.suggestions {
margin: 0;
padding: 0;
position: relative;
/*perspective: 20px;*/
}
.suggestions li {
background: white;
list-style: none;
border-bottom: 1px solid #d8d8d8;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.14);
margin: 0;
padding: 20px;
transition: background 0.2s;
display: flex;
justify-content: space-between;
text-transform: capitalize;
}
.suggestions li:nth-child(even) {
transform: perspective(100px) rotateX(3deg) translateY(2px) scale(1.001);
background: linear-gradient(to bottom, #ffffff 0%, #efefef 100%);
}
.suggestions li:nth-child(odd) {
transform: perspective(100px) rotateX(-3deg) translateY(3px);
background: linear-gradient(to top, #ffffff 0%, #efefef 100%);
}
span.population {
font-size: 15px;
}
.hl {
background: #87aaaa;
}JavaScript
const endpoint =
"https://gist.githubusercontent.com/Miserlou/c5cd8364bf9b2420bb29/raw/2bf258763cdddd704f8ffd3ea9a3e81d25e2c6f6/cities.json";
const cities = [];
fetch(endpoint)
.then((blob) => blob.json())
.then((data) => cities.push(...data));
function findMatches(wordToMatch, cities) {
return cities.filter((place) => {
const regex = new RegExp(wordToMatch, "gi");
// gi means global insensitive
return place.city.match(regex) || place.state.match(regex);
});
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function displayMatches() {
const matchArray = findMatches(this.value, cities);
const html = matchArray
.map((place) => {
const regex = new RegExp(this.value, "gi");
const cityName = place.city.replace(
regex,
`<span class="hl">${this.value}</span>`
);
const stateName = place.state.replace(
regex,
`<span class="hl">${this.value}</span>`
);
return `
<li>
<span class="name">${cityName}, ${stateName}</span>
<span class="population">${numberWithCommas(place.population)}</span>
</li>
`;
})
.join("");
suggestions.innerHTML = html;
}
const searchInput = document.querySelector(".search");
const suggestions = document.querySelector(".suggestions");
searchInput.addEventListener("change", displayMatches);
searchInput.addEventListener("keyup", displayMatches);๐์ฝ๋ ์ค๋ช
- ๊ฐ์ฅ ๋จผ์
fetch()๋ฅผ ํตํด endpoint๋ก๋ถํฐ ์๋ต์ JSON ํํ๋ก ํ์ฑํ๊ณ ๊ทธ๋ ๊ฒ ์ป์ ๋ฐ์ดํฐ๋ฅผcities์ Spread Operator๋ฅผ ์ฌ์ฉํด push ํด์ค๋ค. - ๋ค์์ผ๋ก๋
findMatches()ํจ์๋ฅผ ํตํด cities ๋ฐฐ์ด์ Day 4์์ ์ด์ฉํ๋ filter ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ค. ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋จ์ด์ธ wordToMatch์ ๋น๊ตํ์ฌ ๊ทธ ๋จ์ด๊ฐ city๋ state์ ํฌํจ๋ ๊ฒฝ์ฐ๋ง ํํฐ๋งํด์ค๋ค. ์ฌ๊ธฐ์์ ์ ๊ท ํํ์์ด ์ฌ์ฉ๋๋ค. - ์ด์
searchํด๋์ค์ input์ด ๋ฐ๋ ๋ ๋ง๋ค ๋์๊ฐ ๋ฌ๋ผ์ ธ์ผ ํ๊ธฐ ๋๋ฌธ์change์keyup๋์์ addEventListener๋ฅผ ์ค์ ํ๋ค. ์คํํ ํจ์๋ก๋displayMatches๋กfindMatches(this.value, cities)์์ input๊ณผ ์ผ์นํ๋ ๋์ array๋ฅผmatchArray์ ํ ๋นํ๋ค. ์ดํ์ map ๋ฉ์๋๋ฅผ ํ์ฉํ๋๋ฐ ๋งค์น๋๋ ๋จ์ด๋ฅผ ํ์ด๋ผ์ดํธ ์์ผ์ฃผ๊ธฐ ์ํด์replace๋ฅผ ์ด์ฉํดhlํด๋์ค๋ฅผ ๊ฐ์ง ํ๊ทธ๋ก ๊ฐ์ธ์ค๋ค.
fetch
: ๊ธฐ์กด์ ajax ํต์ ์ ์ฌ์ฉํ ๋ XMLHttpRequest, jQuery.ajax()์ ๊ฐ์ ์์
์ ์ํํ๋ค. return type์ Promise๊ฐ์ฒด์ด๋ค.
let promise = fetch(url, [options])options์ ์๋ฌด๊ฒ๋ ๋๊ธฐ์ง ์์ผ๋ฉด ์์ฒญ์ GET ๋ฉ์๋๋ก ์งํ๋๊ณ url๋ก๋ถํฐ ์ฝํ
์ธ ๊ฐ ๋ค์ด๋ก๋ ๋๋ค.
promise
: JavaScript ๋น๋๊ธฐ ์ฒ๋ฆฌ์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ด๋ค. ์ฃผ๋ก ์๋ฒ์์ ๋ฐ์์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋์์ ์ด์ฉํ ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๋ ์์
์ด ๋๋๊ณ ๋์ ๋์ํด์ผ ์ค๋ฅ๊ฐ ๋ฐ์๋์ง ์์ผ๋ฏ๋ก ์์
์ด ์์ฐจ์ ์ผ๋ก ์๋ฃ๋์ด์ผ ํ๋ค. ์ด๋ฅผ ๋ณด์ฅํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๊ฐ์ฒด๊ฐ promise์ด๋ค.promise๊ฐ์ฒด๋ fetch ์๋ต์ ์ฒ๋ฆฌํ๋ ๋ค์ํ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- response.text() โ ์๋ต์ ์ฝ๊ณ ํ ์คํธ๋ฅผ ๋ฐํํ๋ค.
- response.json() โ ์๋ต์ JSON ํํ๋ก ํ์ฑํ๋ค.
- response.blob() โ ์๋ต์ Blob(ํ์ ์ด ์๋ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ) ํํ๋ก ๋ฐํํ๋ค.
์ ๊ท ํํ์ (RegExp ๊ฐ์ฒด)
: ์ ๊ท ํํ์์ ๋ฌธ์์ด์ ๋ํ๋๋ ํน์ ๋ฌธ์ ์กฐํฉ๊ณผ ๋์์ํค๊ธฐ ์ํด ์ฌ์ฉ๋๋ ํจํด์ด๋ค.
์ด๋ฒ ํ๋ก์ ํธ์์ ์ฐ๋ฆฌ๋ RegExp ๊ฐ์ฒด์ ์์ฑ์ ํจ์๋ฅผ ํธ์ถํด g์ i flag๋ฅผ ์ฌ์ฉํ๋ค. ์ฌ๊ธฐ์ g๋ ๋์ ๋ฌธ์์ด ๋ด์ ๋ชจ๋ ํจํด๋ค์ ๊ฒ์ํ๋ ๊ฒ์ ์๋ฏธํ๊ณ i๋ ๋์ ๋ฌธ์์ด์ ๋ํด์ ๋/์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ์ง ์๋ ๊ฒ์ ์๋ฏธํ๋ค.
๐TIL(Today I Learned)
- JavaScript์ ๋น๋๊ธฐ ํต์ ์ ๋ํด ๋ ์์๋ด์ผ ํ ํ์์ฑ์ ๋๋ ์ ์์๋ค.
fetch,promise(๊ฐ์ฒด),RegExp,addEventListener(change),addEventListener(keyup)
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [JS] ๐๏ธJavaScript 30 - Day 8 (0) | 2021.11.22 |
|---|---|
| [JS] ๐๏ธJavaScript 30 - Day 7 (0) | 2021.11.21 |
| [JS] ๐๏ธJavaScript 30 - Day 5 (0) | 2021.11.18 |
| [JS] ๐๏ธJavaScript 30 - Day 4 (0) | 2021.11.17 |
| [JS] ๐๏ธJavaScript 30 - Day 3 (0) | 2021.11.12 |
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋ค๋ฅธ ๊ธ
-
[JS] ๐๏ธJavaScript 30 - Day 8
[JS] ๐๏ธJavaScript 30 - Day 8
2021.11.22 -
[JS] ๐๏ธJavaScript 30 - Day 7
[JS] ๐๏ธJavaScript 30 - Day 7
2021.11.21 -
[JS] ๐๏ธJavaScript 30 - Day 5
[JS] ๐๏ธJavaScript 30 - Day 5
2021.11.18 -
[JS] ๐๏ธJavaScript 30 - Day 4
[JS] ๐๏ธJavaScript 30 - Day 4
2021.11.17