-
320x100
Document ๐ฅJavascript 30 - Day 06๐ฅ
์ด๋ฒ์๋ Javascript30์ Day 06์ ํด๋นํ๋ 'Ajax Type Ahead'๋ฅผ ์ ๋ฆฌํด๋ด ๋๋ค@@@@
json ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
์ฐ์ ์ json ๋ด์ฉ์ ๊ฐ์ ธ์ ๋ด ์๋ค.
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));์ด๊ฒ ๋ญ ์๋ฆฐ๊ฐ ์ถ์ฃ ..... ํ๋ํ๋ ์ดํด๋ณด๋ฉด ๋ฉ๋๋ค. fetch๋ถํฐ ๊ถ๊ถ.
fetch()๋, ์ฝ๊ฒ ๋งํ๋ฉด ํด๋ผ์ด์ธํธ๊ฐ ์๋ฒ์๊ฒ ์ด๋ค ํ์ผ์ ์๋ตํด๋ฌ๋ผ๊ณ ์์ฒญํ๋ ๊ฒ์ ๋๋ค.fetch('hello')๋ hello๋ผ๋ ํ์ผ์ ์์ฒญํ ๊ฒ ๋์ฃ . ์ด๊ฒ์ ๋น๋๊ธฐ๋ก ์ด๋ฃจ์ด์ง๊ธฐ ๋๋ฌธ์, fetch๋ฅผ ํ๋ ๋์ ๋ค๋ฅธ ์ฝ๋๋ฅผ ์คํํ๊ฒ ๋ฉ๋๋ค. ๊ทธ ์์ ์ด ๋๋๋ฉด '~ํด์ค!'๋ผ๊ณ ํ ๋ ์ฐ๋ ๊ฒ, ์ฝ๋ฐฑํจ์๋ฅผ ์คํ์์ผ์ฃผ๋then()์ ๋๋ค. ์ด ์ฝ๋ฐฑํจ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก๋ response ๊ฐ์ฒด๊ฐ ๋ค์ด๊ฐ๋๋ค. ์ ๊ฐ์ฒด๋ ์๋ฒ๊ฐ ์๋ตํ ๊ฒฐ๊ณผ๋ฅผ ๋ด๊ณ ์์ด์!์๋ฅผ ๋ค์ด, abc.txt๋ผ๋ ํ์ผ์ด ์๊ณ ์๋์ ์ฝ๋๋ฅผ ์คํํ๋ค๊ณ ํฉ์๋ค.
fetch("abc.txt").then(function (resp) { resp.text() .then(function (text) { console.log(text); }); })๊ทธ๋ผ ์ฝ์์๋ abc.txt์ ๋ด์ฉ์ด ์ฐํ๊ฒ ๋ฉ๋๋ค.
์ด๋ฒ์๋ txt ํ์ผ์ด ์๋ color.json ํ์ผ๋ก ์๋ํด๋ด ์๋ค.
[{ "color": "red", "value": "#f00" }, { "color": "green", "value": "#0f0" }, { "color": "blue", "value": "#00f" }, { "color": "cyan", "value": "#0ff" }, { "color": "magenta", "value": "#f0f" }, { "color": "yellow", "value": "#ff0" }, { "color": "black", "value": "#000" } ]fetch(`data/color.json`).then(function (blob) { blob.json() .then(function (data) { console.log(data); }); })์ง , ์ฝ์์ ์ ์ฐํ๋์ค๋ค์! ๋ฌ๋ผ์ง ๊ฑด ํ์ผ ์ด๋ฆ๊ณผ, text() ๋์ json() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค๋ ๊ฑฐ์ฃ .
์ฌ์ฌ ๋งจ ์ฒ์ ์์ฑํ๋ ์ฝ๋๊ฐ ๊ฐ์ด ์ฌ๋ฝ๋ง๋ฝ ํฉ๋๋ค. endpoint์ ํด๋นํ๋ json์ ๋ด์ฉ์ fetchํ ๋ค์, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด์๋ค ๋ฃ๋ ๊ฑฐ์๋ค์. ์, ๊ทผ๋ฐ ์ด์ํ ํํ์ด ํ๋ ์์ด์.cities.push(...data)๋ ๋ญ๊น์?์ด๊ฑด ์ ๊ฐ ์ฐ์ฐ์(spread operator)๋ผ๊ณ ํ๋ฉฐ, ES6์์ ์ถ๊ฐ๋์ต๋๋ค. MDN์ ๋ฐ๋ฅด๋ฉด ์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ ์ ์๋ค๊ณ ํ๋ค์.
var parts = ['shoulders', 'knees']; var lyrics = ['head', ...parts, 'and', 'toes']; //["head", "shoulders", "knees", "and", "toes"]์ด๋จธ๋ ๊ธฐ์กด ๋ฐฐ์ด์ธ parts๊ฐ ์ ๋ฐฐ์ด์ lyrics ์ผ๋ถ๋ก ์ ๋ น์๋ค์๋ค์! ๊ทธ๋ผ ์ด๋ ๊ฒ๋ ์ฌ์ฉํ ์ ์๊ฒ ๋ค์.
var arr1 = [0, 1, 2]; var arr2 = [3, 4, 5]; arr1.push(...arr2);๋ฐฐ์ด์ด ์ ํฉ์ณ์ก๋ค์ :^)
๊ทธ๋ผ ์ด๋ ๋๋ ์๊ฐ. ์ด๋ผ? ๊ทธ๋ฅarr1.push(arr2);ํ๋ฉด ๋์ง ์์? ...๋ผ๊ณ ์๊ฐํ์ต๋๋ค๋ง... ๊ฒฐ๊ณผ๋ ์ด๋จ๊น์?๋๊ฐ ์ฌ๋ฐ ๋ฐฐ์ด ์ด๋ฐ๊ตฌ๋ก ์ฐ๊ฒจ๋ฃ์์ด!!!๋ต. ๊ทธ๋ฅ ๋ฐฐ์ด์ด ๋ฉ๊ทธ๋ฌ๋ ๋ค์ด๊ฐ๋๋ค. ๊ทธ๋ผ ์ ์ด์ด๊ธฐ ์์ ์๋ ์ฝ๋์์ ์ ๊ฐ์ฐ์ฐ์๋ฅผ ๋นผ๊ณ ,
.then(data => cities.push(data));๋ผ๊ณ ์ผ๋ค๋ฉด ๊ฒฐ๊ณผ๋ ์๋์ ๊ฐ๊ฒ ์ฃ ?ํผ๋ํ๋ค ํผ๋ํด๋ฐ๋ผ์ ์ ๊ฐ์ฐ์ฐ์๋ฅผ ์จ์ ๊ธฐ์กด ๋ฐฐ์ด์ธ cities์๋ค๊ฐ ์ ์ง์ด๋ฃ์ด์ค ๊ฒ ๋ฉ๋๋ค (์ง์ง์ง)
findMatches() ํจ์ ์์ฑํ๊ธฐ
์ด๋ฒ์๋ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋จ์ด๊ฐ ๋ฐ์ดํฐ ์ค์ ์๋์ง ํ๋จํ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค. ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฒฝ์ฐ ๊ทธ ๊ฐ๋ง ๋ชจ์ ์ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ฃผ๋
filter()๋ฉ์๋๊ฐ ํ์ํ๊ฒ ๊ตฐ์.
๊ทธ ์กฐ๊ฑด์ ๋ฌธ์์ด์ด ์๋๊ฐ๋ฅผ ํ๋จํ๋ ๊ฑด๋ฐ์, ์ด๋๋match()๋ฉ์๋๋ฅผ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค. match()๋ ์ ๊ทํํ์์ ๋ง๋ ๋ฌธ์์ด์ ์ฐพ์์ ๋ฐฐ์ด ๊ฐ์ฒด๋ก ๋ฐํํฉ๋๋ค. ์ด๋, ๋ฐฐ์ด์ด ์๋ ๋ฌธ์์ด์ ๋์์ผ๋ก ํจ์ ์ฃผ์ํฉ์๋ค. ์ฌ๊ธฐ์ ์ ๊ทํํ์์ ๋ ผํ๊ธฐ์ ์คํฌ๋กค์ด ๋ถ์กฑํ๋ฏ๋ก ์ค๋ฃจํ๊ณ ๊ฒ์ํด์ ์ฌ์ฉํด๋ด ๋๋น. ์ผ๋จ ๊ฐ์์์ ์์ฑํ ์ฝ๋๋ฅผ ์ดํด๋ด ๋๋ค.function findMatches(wordToMatch, cities) { return cities.filter(place => { const regex = new RegExp(wordToMatch, "gi"); return place.city.match(regex) || place.state.match(regex); }); }findMatches()๋ผ๋ ํจ์๋ฅผ ์์ฑํ๋๋ฐ ์ธ์ 2๊ฐ๊ฐ ํ์ํฉ๋๋ค. ํ๋๋ ์ฌ์ฉ์๊ฐ ๊ฒ์ํ ๋จ์ด๊ณ , ํ๋๋ ์ ์ฒด ๋ฐฐ์ด์ด๋ค์. filter() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ ์๊ณ , match() ๋ฉ์๋๋ ๋ณด์ ๋๋ค. ํํธ ์ฌ๊ธฐ์๋ ์ ๊ท์์
new RegExp()๋ผ๋ ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ฐ๊ณ ์๋ค์. ์ gi๋ global match์ ignore case๋ฅผ ๋ปํ๋ค๊ณ ํฉ๋๋ค. ์์ ๋ชจ๋ฅด๊ฒ ์ง๋ง MDN์ด ๊ทธ๋ ๋ค๋ค์ใ _ใ ํํธ, place๊ฐ city์ state ๋ ๊ฐ์ด๊ธฐ ๋๋ฌธ์ || ์ฐ์ฐ์๋ฅผ ์ฐ๊ณ ์๋ ๊ฒ๋ ์ฒดํฌ!!๋ค๋ฅธ ์์ ๋ก ์ข ๋ ์ฝ๊ฒ ์์๋ด ์๋ค.
var arr = [1, 2, 3, 'red', 'yellow', 'blue', 'black', 'brown']; var strArr = arr.map(function (e) { return e.toString() });์ฌ๊ธฐ arr๋ผ๋ ๋ฐฐ์ด์ด ์์ต๋๋ค. ์ด๊ฑธ ๊ฐ์ง๊ณ findMatches() ํจ์์ ์ ์ฉ์ํฌ ๊ฑด๋ฐ, ํ๋ ์ฃผ์ํ ์ ! map() ๋ฉ์๋๋ฅผ ํตํด ๊ฐ ์์๋ณ๋ก toString()์ ์ ์ฉ์์ผ strArr๋ผ๋ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์๋๋ฐ์, ์ด๋ match()๋ฉ์๋๋ String์๋ง ์ธ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค. ๋ฐฐ์ด ๋ด 1,2,3์ ์ซ์๋๊น ๋ฌธ์์ด๋ก ๋ฐ๊ฟ ์ค ํ์๊ฐ ์์ฃ !
์ด์ด์ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
function findMatches(array) { return array.filter(data => { const regex = new RegExp("e", "gi"); console.log(data.match(regex)); }); } findMatches(strArr);์ฝ์์ ์ฐํ ๊ฒฐ๊ณผ๋ ์ ์ด๋ฏธ์ง๋๋ก ์ ๋๋ค. match()๋ฉ์๋๊ฐ ๊ฐ data๋ง๋ค "e"๋ผ๋ ๋ฌธ์๊ฐ ์๋์ง ํ๋จํด์ ์์ผ๋ฉด ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ๋ฆฌํด, ์์ผ๋ฉด null์ ๋ฆฌํดํด์ฃผ๋ค์. ์ ํจ์๋ฅผ ์ข ๋ ๋ณด์ํฉ์๋ค. ์ฝ์์ ์ฐ๋ ๋์ filter()๊ฐ ์ ๋ฐฐ์ด์ ๋ฆฌํดํ๊ฒ ํ๊ณ , ํ๋จํ๋ ๋ฌธ์์ด์ ์ธ์๋ก ๋์๊ตฌ์!
function findMatches(word, array) { return array.filter(data => { const regex = new RegExp(word, "gi"); return data.match(regex); }) } var newArr = findMatches('o', strArr); console.log(newArr);(์ง์ง์ง)
์ฌ์ฉ์ ์ ๋ ฅํ ๋จ์ด๋ก ๊ฒฐ๊ณผ๊ฐ ์ฐพ๊ธฐ
์ฌ์ฉ์๊ฐ input์ ์ ๋ ฅํ๋ ๊ฐ์ ๊ฐ์ ธ์ค๊ณ , findMatches() ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๋ ค๊ณ ํฉ๋๋ค. ์ผ๋จ์ input๊ณผ ๊ฒฐ๊ณผ์ฐฝ์ ์ ๋ ํ ํ๊ณ , input์๊ฒ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๊ฑธ์ด์ค๋๋ค.
function displayMatches(){ console.log(this.value); } const searchInput = document.querySelector(".search"); const resultArea = document.querySelector(".result"); searchInput.addEventListener("change", displayMatches); searchInput.addEventListener("keyup", displayMatches);๊ทธ๋ผ displayMatches()๋ด์ findMatches()๋ฅผ ์ฌ์ฉํด๋ด ๋๋ค! ์ธ์๋ this.value์ ๋ฐฐ์ด์ด ๋ค์ด๊ฐ๋ฉด ๋๊ฒ ์ฃ ? ์ฌ๊ธฐ์ ๋ณด๊ธฐ ์ฝ๊ฒ strArr๋ฅผ ๊ทธ๋๋ก ๊ฐ์ ธ๋ค ์จ๋ณด๊ฒ ์ต๋๋ค.
function displayMatches() { const matchArray = findMatches(this.value, strArr); console.log(matchArray); }๊ทธ๋ผ input์ฐฝ์ "r"์ ์ ๋ ฅํ๋ฉด ์ฝ์์ ["red", "brown"]์ด๋ผ ์ฐํ๋ ๊ฑธ ๋ณผ ์ ์์ต๋๋ค.
์ด์ ์ด ๋ฐฐ์ด์ html ํํ๋ก ๋ง๋ค๋ฉด ๋๊ฒ ๋ค์. map()์ ํ์ฉํด์ ใฑใฑconst html = matchArray.map(color => { return ` <li> ${color}</li> `; }); console.log(html);๋ฌผ๋ก ์ผ์ผ ์์ฑํ๋ฉด ์ ๋๋ก ์ ๋์ต๋๋ค. ์ฝ์์ ์ดํด๋ณด๋ฉด html์ด ๋ฐฐ์ด์ด๋ ๊ฑธ ์ ์ ์๊ฑฐ๋ ์. map()์ ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํด์ฃผ๋๊น์! ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๊ฑด ๋ฌธ์์ด์ด๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์
join()์ ์ฌ์ฉํฉ๋๋ค. join()์ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ฐ๊ฒฐํด ํ๋์ ๋ฌธ์์ด๋ก ๋ง๋ค์ด์ฃผ๋ฉฐ, ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๊ฒ ์ต๋๋น.
๋ฐ๋ผ์ map() ๋์ join()์ ์ดํฌ์ ์ถ๊ฐํ๋ฉด ์ ์์ ์ผ๋ก ์ถ๋ ฅ๋ฉ๋๋ค. ์ด๋ ๋งค๊ฐ๋ณ์๋ก ์๋ฌด๊ฒ๋ ์์ผ๋ฉด ์ผํ๋ก ๊ตฌ๋ถ๋๊ณ , ๋น ๋ฌธ์์ด("")์ ๋ฃ์ผ๋ฉด ๊ตฌ๋ถ์์ด ๋งค๋๋ฝ๊ฒ ์ฐ๊ฒฐ๋๋ค๋ ์ !const html = matchArray.map(color => { return ` <li>${color}</li> `; }).join(""); resultArea.innerHTML = html;
์ ํํ ๋จ์ด ๊ฐ์กฐ ํ์ํ๊ธฐ
๋ง์ง๋ง ํ ์ผ์ ์ฌ์ฉ์๊ฐ ์ ๋ ฅํ ๋จ์ด๋ฅผ ๊ฒฐ๊ณผ๊ฐ์์ ๊ฐ์กฐ ํ์ํ๋ ์ผ์ ๋๋ค. ์ฌ์ฉ์๊ฐ re๋ฅผ ์ ๋ ฅํ์ผ๋ฉด red์์ re๋ถ๋ถ๋ง ๊ฐ์กฐํด์ฃผ๋ ๊ฑฐ์ฃ .
๊ทธ๋ฌ๊ธฐ ์ํด์๋ html์ ๋ฆฌํดํ๊ธฐ ์ ์ ํ ๋ฒ ๋ ํ ์ผ์ด ์์ต๋๋ค. ๋ฐ๋ก ๋ฌธ์๋ฅผ ์ชผ๊ฐ๋ ๊ฑฐ์ฃ . ์ฝ๊ฒ '์ฌ์ฉ์๊ฐ ๊ฒ์ํ ๋จ์ด+๋๋จธ์ง ๋จ์ด'๋ก ๋ฐ์ด...์ด ์๋๋ผ ๋ถ๋ฆฌํ๋ฉด ๋์ง ์์๊น์?์ด๋ ์ธ ๋ฉ์๋๋
replace()์ ๋๋ค. ๋ฌธ์์ด์ ๊ต์ฒดํ ๋ ์ฌ์ฉํฉ๋๋ค. (*์ฐธ๊ณ )var str = "Anne is awesome"; var newStr = str.replace(/anne/i, "nykim"); console.log(newStr); //nykim is awesome;์ด๊ฑธ ์์ฉํ๋ฉด ์๋์ ๊ฐ์ต๋๋ค.
function displayMatches() { const matchArray = findMatches(this.value, strArr); console.log(matchArray); const html = matchArray.map(color => { const regex = new RegExp(this.value, "gi"); const colorName = color.replace( regex, ` <span class="accent">${this.value} </span>` ); return ` <li>${colorName} </li> `; }).join(""); resultArea.innerHTML = html; }์, ํ ๊ฐ์ง ํ ์ผ์ด ๋ ์์๋ค์. ์ ~~~ ์์ endpoint์์ ๊ฐ์ ธ์จ ๋ฐ์ดํฐ์๋ ์ธ๊ตฌ ์ ๋ณด๊ฐ ์๋๋ฐ, ์ด๊ฑธ 3์๋ฆฌ๋ง๋ค ์ผํ๋ฅผ ๋ถ์ฌ์ ํ์ํ๋ ค๊ณ ํฉ๋๋ค.
์ด๊ฒ ์ญ์ repalce()์ ์ ๊ท์์ ํ์ฉํ ๊ฑด๋ฐ, ๊ตฌ๊ธ๋งํด์ ์ฒซ๋ฒ์งธ๋ก ๋์ค๋ ๊ฑฐ ๊ฐ์ ธ์์ต๋๋ค ํํfunction numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }
์ต์ข ์ฝ๋
Bos์จ๊ฐ ์์ฑํ ์ต์ข ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
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 => { // here we need to figure out if the city or state matches what was searched const regex = new RegExp(wordToMatch, "gi"); return place.city.match(regex) || place.state.match(regex); }); } var aa = findMatches("e", cities); console.log(aa); 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);๋!
728x90'Blog > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JS30] Dev Tools Tricks (0) 2019.02.17 [JS30] HTML Canvas (0) 2019.02.15 [JS30] Array ๋ฉ์๋(2) (0) 2019.02.11 [JS30] Array ๋ฉ์๋ (1) (1) 2019.01.07 [JS30] ์๋ฐ์คํฌ๋ฆฝํธ๋ก CSS ๋ณ์(์ฌ์ฉ์ ์ ์ ์์ฑ) ์กฐ์ ํ๊ธฐ (5) 2019.01.07 ๋๊ธ