๐ ์๊ตฌ์ฌํญ
- input์ฐฝ์ ๋ฌธ์๋ฅผ ์ ๋ ฅํ๋ฉด, ์ผ์นํ๋ city๋ state์ ๊ฐ์ ํ๋จ์ ์ถ๋ ฅํ๋ค.
โจ ๊ฒฐ๊ณผํ๋ฉด
๐ป ์์ค์ฝ๋
๐ ๋ด๊ฐ ์์ฑํ ์ฝ๋
๋ฉฐ์น ๊ฐ ์ด๊ฒ์ ๊ฒ ์ฐพ์๋ณด๋ฉด์ ํค๋งธ๋๋ฐ, json ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ city, state, population ๋ฐ์ดํฐ๋ฅผ ๋ฝ์๋ด๊ณ if๋ฌธ์ผ๋ก ์กฐ๊ฑด์ ์ฃผ๋ ๊ฒ๊น์ง ์์ฑํ๋ค. ๊ทธ๋ฐ๋ฐ ๋ด๊ฐ ์ด ์ฝ๋๋ ํ li์์ ์ถ์ถ๋ ๋ชจ๋ object๊ฐ ๋ค์ด๊ฐ๋(;;;) ๋ฌธ์ ๊ฐ ์์๋ค. ๊ฒฐ๋ก ์ ์ ๋ฐ์ ์ผ๋ก ์๋ฒฝํ๊ฒ ์์ฑํ์ง ๋ชปํ๋ค. ๊ทธ๋์ ๊ฐ์๋ฅผ ๋ณด๊ณ ๋ฐ๋ผ ์ณ๋ณด๋ฉด์ ์์ฑ์์ผ๋ณธ ๋ค์, ๊ฐ์ธ์ ์ผ๋ก ๋ค์ ํ ๋ฒ ํ์ด๋ณด๊ธฐ๋ก ํ๋ค.
๐ค ๊ฐ์ ๋ ๊ฐ์ ์ฝ๋
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Type Ahead ๐</title>
<link rel="stylesheet" href="style.css" />
<script src="before.js" async></script>
</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>
</body>
</html>
JS
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) => {
// ์
๋ ฅ๊ณผ ์ผ์นํ๋ city๋ state ์ฐพ๊ธฐ
const regex = new RegExp(wordToMatch, "gi");
return place.city.match(regex) || place.state.match(regex);
});
}
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function displayMatched() {
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", displayMatched);
searchInput.addEventListener("keyup", displayMatched);
๐ TIL
.fetch()
fetch() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ๋คํธ์ํฌ ํต์ ์ ์๊ธฐ์ฝ๊ฒ ๊ธฐ์ ํ ์ ์๋ค. ๊ทธ๋ฐ๋ฐ, ๋จ์ํ fetch()๋ง ์ฌ์ฉํ๊ฒ ๋๋ฉด ๊ทธ๋ฅ ๋จ์ํ Promise ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค. ๊ทธ๋์ json() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด, HTTP Response์ body์ ์๋ ํ ์คํธ๋ฅผ JSON ํํ๋ก ๋ณํํด์ฃผ๊ณ , ์ด๊ฒ์ ์ฌ์ฉํ ์ ์๊ฒ ๋๋ค. ๊ทธ๋์ ๋ณดํต ๋ค์๊ณผ ๊ฐ์ ํํ๊ฐ ๋ํดํธ๋ก ์ฌ์ฉ๋๋ค.
fetch('http://example.com/movies.json')
.then((response) => response.json())
.then((myJson) => console.log(JSON.stringify(myJson)));
API ํธ์ถ์ด ์คํจํ์ ๋๋, catch()๋ฅผ ํตํด ์์ธ(error) ๊ฐ์ฒด๋ฅผ rejectํ ์ ์๋ค. Promise์ ๋ํ ๊ฒ์ ์ฌ๊ธฐ์ ์ ๋ฆฌํด ๋์์ผ๋, ํ๋ฒ ๋ณต์ตํด๋ณด๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ๋ค. ๊ฐ์์์๋ ์์ ๊ฐ์ ํ์์ผ๋ก JSON์ ๊ฐ์ ธ์จ ํ, cities๋ผ๋ ๋น ๋ฐฐ์ด์ push()ํ์ฌ ๋ฃ๊ณ ์๋ค. ๊ทธ๋ฐ๋ฐ ์ฝ๋๋ฅผ ์์ธํ ๋ณด๋ฉด, ์ด์ํ๊ฒ ๋์ ๋๋ค.
fetch(endpoint)
.then((blob) => blob.json())
.then((data) => cities.push(...data));
๊ทธ๋ฅ push(data)๋ฅผ ํด์ค๊ฒ ์๋๋ผ ์ธ์ ์์ ์ ์ธ๊ฐ(...)๊ฐ ๋ถ์ด์๋ค. ์ด๊ฒ ๋ญ์ง ๊ถ๊ธํด์ ์ฐพ์๋ดค๋ค.
Spread Operator(...์ฐ์ฐ์)
Spread ์ฐ์ฐ์๋ ES6์์ ์๋กญ๊ฒ ๋ฑ์ฅํ ๋ฌธ๋ฒ์ด๋ค. Spread ์ฐ์ฐ์๋ ๋ฐฐ์ด, ๋ฌธ์์ด ๋ฑ iterable object์ element๋ฅผ ํ๋์ฉ ๋ถ๋ฆฌํ์ฌ ์ ๊ฐํ๊ธฐ ์ํด ์ฌ์ฉํ๋ค. iterable object๋ ๋ฐ๋ณต ๊ฐ์ฒด๋ก์จ, ๊ฐ์ฒด์ ์์์ ํ๋์ฉ ์ฐจ๋ก๋ก ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค. MDN์์๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋์ด ์๋ค.
์ ๊ฐ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ด๋ ๋ฌธ์์ด๊ณผ ๊ฐ์ด ๋ฐ๋ณต ๊ฐ๋ฅํ ๋ฌธ์๋ฅผ 0๊ฐ ์ด์์ ์ธ์ (ํจ์๋ก ํธ์ถํ ๊ฒฝ์ฐ) ๋๋ ์์ (๋ฐฐ์ด ๋ฆฌํฐ๋ด์ ๊ฒฝ์ฐ)๋ก ํ์ฅํ์ฌ, 0๊ฐ ์ด์์ ํค-๊ฐ์ ์์ผ๋ก ๊ฐ์ฒด๋ก ํ์ฅ์ํฌ ์ ์์ต๋๋ค.
์ ์ดํด๊ฐ ๋์ง ์์์ ์ข ๋ ์ฐพ์๋ณด์๋ค. ์๋์ ๊ฐ๋จํ ์์๊ฐ ์๋ค.
const arr = [1, 2, 3, 4, 5];
console.log(arr); // [1, 2, 3, 4, 5]
console.log(...arr); // 1 2 3 4 5
์์๋ฅผ ๋ณด์์ ๋, Spread ์ฐ์ฐ์๋ ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ชผ๊ฐ๋ ๊ฒ ๊ฐ๋ค.
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [...arr1, 6, 7, 8];
const arr3 = [...arr1, 6, 7, 8, ...arr1];
console.log(arr1); // [1, 2, 3, 4, 5]
console.log(arr2); // [1, 2, 3, 4, 5, 6, 7, 8]
console.log(arr3); // [1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5]
๋, ๊ธฐ์กด์ Object๋ ๊ฑด๋๋ฆฌ์ง ์์ผ๋ฉด์๋ ๊ทธ๊ฒ์ ๋ณต์ฌํด์ ํน์ ํญ๋ชฉ์ ์๋กญ๊ฒ ์ง์ด๋ฃ์ ์ ์๋ค. ์ด ๋, ์๋ณธ ๊ฐ์ฒด ๋ด์ ์์๋ฅผ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ์๋ ์๋ก์ด ๊ฐ์ฒด ์์ ์์ ๊ฐ๋ ๋ณ๊ฒฝ๋๋ค. ์ด๋ฌํ Spread ์ฐ์ฐ์๋ ๋์ฒด๋ก ๊ฐ์ฒด ๋ณต์ฌ, ๊ฐ์ฒด ๋ณํฉ ๋ฑ์ ์ฌ์ฉ๋๋ ๊ฒ ๊ฐ๋ค.
RegExp
RegExp๋ ๋ฌธ์์ด์ ํฌํจ๋ ํน์ ๋ฌธ์ ์กฐํฉ๊ณผ ๋์์ํค๊ธฐ ์ํด ์ฌ์ฉ๋๋ ํจํด์ด๋ค. ์ ๊ท ํํ์์ ๊ตฌํํ JavaScript ํ์ค ๋ด์ฅ ๊ฐ์ฒด์ด๊ณ , ์์ฑํ๋ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
new RegExp(๊ฒ์ํจํด[, ํ๋๊ทธ]);
ํ๋๊ทธ๋ ์ ๊ทํํ์์ ๊ฒ์ ๋ฐฉ์์ ์ค์ ํ๊ธฐ ์ํด ์ฌ์ฉํ๋๋ฐ, ํ๋ ์ด์์ ํ๋๊ทธ๋ฅผ ๋์์ ์ค์ ํ ์ ์๋ค. ๋ํ์ ์ธ 3๊ฐ์ง์ ํ๋๊ทธ๋ฅผ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
i(ignore case) | ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์๊ณ ํจํด์ ๊ฒ์ํ๋ค. |
g(global) | ๋์ ๋ฌธ์์ด ๋ด์์ ํจํด๊ณผ ์ผ์นํ๋ ๋ชจ๋ ๋ฌธ์์ด์ ์ ์ญ ๊ฒ์ํ๋ค. |
m(multi line) | ๋ฌธ์์ด์ ํ์ด ๋ฐ๋๋๋ผ๋ ํจํด ๊ฒ์์ ๊ณ์ํ๋ค. |
๐ ์ฐธ๊ณ
youtube Wes Bos
https://www.youtube.com/watch?v=y4gZMJKAeWs&list=PLu8EoSxDXHP6CGK4YVJhL_VWetA865GOH&index=6
https://developer.mozilla.org/ko/docs/Web/API/Fetch_API/Using_Fetch
'language > javascript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript30] 7. Array Cardio Practice (2) | .some(), .every(), .find() and [...SPREADS] (0) | 2022.02.05 |
---|---|
[JavaScript30] 5. Flexbox + JavaScript Image Gallery (0) | 2021.12.10 |
[JavaScript30] 4. Array Cardio Practice (1) (0) | 2021.12.09 |
[JavaScript30] 3. CSS Variables | CSS ๋ณ์, setProperty() (0) | 2021.12.06 |
[JavaScript30] 2. Clock | setInterval(), transform, transition (0) | 2021.12.04 |