type ahead

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)); // ๋ฐ์ดํ„ฐ 
//json ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ 
// fetch -> ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„๋ ˆ ์–ด๋–คํŒŒ์ผ์„ ์‘๋‹ตํ•ด๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•œ๊ฒƒ


function findMatches(wordToMatch, cities) {
  return cities.filter(place => {
    //๋„์‹œ๋‚˜ ์ฃผ๊ฐ€ ๊ฒ€์ƒ‰ํ•œ ๊ฒƒ๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธํ›„ ์ผ์น˜ํ•˜๋Š” ๋‹จ์–ด๋งŒ ์ƒˆ ๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
    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 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()

  1. ์„œ๋ฒ„์—๊ฒŒ ์–ด๋–คํŒŒ์ผ์„ ์‘๋‹ตํ•˜๋ผ๊ณ  ์š”์ฒญํ•˜๋Š”๊ฒƒ์ด๋‹ค.

  2. ๋น„๋™๊ธฐ๋กœ ์ด๋ฃจ์–ด์ง€๊ธฐ๋•Œ๋ฌธ์— fetch ๋˜๋Š” ๋™์•ˆ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

  3. ๊ทธ ์ž‘์—…์ด ๋๋‚˜๊ณ ๋‚˜์„œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ์ฃผ๋Š”๊ฒƒ์„ ๐Ÿ‘‰ then()์ด๋ผ๊ณ ํ•œ๋‹ค.

  4. response ๊ฐ์ฒด ๊ฐ€ ๋“ค์–ด๊ฒŒ ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ ์•ˆ์—๋Š” ์„œ๋ฒ„๊ฐ€ ์‘๋‹ตํ•œ ๊ฒฐ๊ณผ๊ฐ’์ด ์žˆ๋‹ค.

fetch(`member.json`).then(function (blob) {
  blob.json()
    .then(function (data) {
      console.log(data);
      console.log('hello');
  });
})

.push

const parts = ['knoo', 'shoo'];
let lyrics = ['head', ...parts, 'and', 'toes']; //["head", "shoulders", "knees", "and", "toes"]

const arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];
arr1.push(...arr2); // 0 , 1, 2, 3, 4, 5 

console.log(lyrics);
console.log(arr1);

findMatches()

function findMatches(wordToMatch, cities) {
  return cities.filter(place => {
    const regex = new RegExp(wordToMatch, "gi");
    return place.city.match(regex) || place.state.match(regex);
    //  city์™€ state ๋‘๊ฐœ ์ด๊ธฐ๋•Œ๋ฌธ์— || ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ 
  });
}
  • findMatches() ์ธ์ž 2๊ฐœ๊ฐ€ ํ•„์š”ํ•จ ํ•˜๋‚˜๋Š” ๊ฒ€์ƒ‰ํ•œ ๋‹จ์–ด, ํ•˜๋‚˜๋Š” ์ „์ฒด ๋ฐฐ์—ด

  • RegExp ๐Ÿ‘‰ ๊ฐ์ฒด๋Š” ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ•๊ณผ ์ƒ์„ฑ์ž๋กœ์จ ์ƒ์„ฑํ• 

  • ๋ฆฌํ„ฐ๋Ÿด ํ‘œ๊ธฐ๋ฒ• ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋‘ ๋น—๊ธˆ์œผ๋กœ ๊ฐ์‹ธ์•ผ ํ•˜๋ฉฐ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ๋น—๊ธˆ์œผ๋กœ ๊ฐ์‹ธ์ง€ ์•Š์œผ๋‚˜ ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  • gi ๐Ÿ‘‰ global match์™€ ignore case๋ฅผ ๋œปํ•จ

์‚ฌ์šฉ์ž ์ž…๋ ฅํ•œ ๋‹จ์–ด๋กœ ๊ฒฐ๊ณผ๊ฐ’ ์ฐพ๊ธฐ

function displayMatches(){
  console.log(this.value);
}
 
const searchInput = document.querySelector(".search");
const resultArea = document.querySelector(".result");
 
searchInput.addEventListener("change", displayMatches);
searchInput.addEventListener("keyup", displayMatches);

/*์‚ฌ์šฉ์ž๊ฐ€ input์— ์ž…๋ ฅํ•˜๋Š” ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ณ , findMatches() ๊ฒฐ๊ณผ๋ฅผ ๋ณด. 
input๊ณผ ๊ฒฐ๊ณผ์ฐฝ์„ ์…€๋ ‰ํŒ…ํ•˜๊ณ , input์—๊ฒŒ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๊ฑธ์–ด์ค€๋‹ค.*/


function displayMatches() {
  const matchArray = findMatches(this.value, strArr); // this.value ์™€ ๋ฐฐ์—ด์ด 
  console.log(matchArray);
}

Last updated