๐Ÿ“”
web_study
  • JavaScript
  • ์—˜๋ฆฌ_์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ณธ
      • ํด๋ž˜์Šค ์˜ˆ์ œ์™€ ์ฝœ๋ฐฑํ•จ์ˆ˜
      • ์—ฐ์‚ฐ์ž boolean
      • ํ•จ์ˆ˜ ์ •์˜, ํ˜ธ์ถœ ๊ทธ๋ฆฌ๊ณ  ์ฝœ๋ฐฑ
      • ๋ณ€์ˆ˜ํƒ€์ž…๊ณผ object ์ฐจ์ด์ 
    • JavaScript async ์™€ await
    • JavaScript Promise
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ callback
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ json
    • ์œ ์šฉํ•œ 10๊ฐ€์ง€ ๋ฐฐ์—ด ํ•จ์ˆ˜๋“ค
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด(array)
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ object
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ (class vs object) ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—ฐ์‚ฐ.๋ฐ˜๋ณต๋ฌธ
    • ๋ฐ์ดํ„ฐํƒ€์ž…, data types, let vs var, hoisting
    • script async ์™€ defer์˜ ์ฐจ์ด์  ์ฝ˜์†” ์ถœ๋ ฅ
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—ญ์‚ฌ
  • ์ƒํ™œ์ฝ”๋”ฉ
    • ์žฌ๊ท€ํ•จ์ˆ˜
    • ์ •๊ทœ ํ‘œํ˜„์‹
    • ๊ฐ์ฒด์ง€ํ–ฅ
      • ๊ฐ์ฒด์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ
      • ์ƒ์„ฑ์ž์™€ new
      • ์ „์—ญ๊ฐ์ฒด
      • this
      • ์ƒ์†
      • prototype
      • ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด์˜ ํ™•์žฅ
      • object
      • ๋ฐ์ดํ„ฐ ํƒ€์ž…
      • ์ฐธ์กฐ
    • ํ•จ์ˆ˜์ง€ํ–ฅ
      • ์œ ํšจ๋ฒ”์œ„
      • ๊ฐ’์œผ๋กœ์„œ์˜ ํ•จ์ˆ˜์™€ ์ฝœ๋ฐฑ
      • ํด๋กœ์ €
      • arguments
      • ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ
    • UI API, ๋ฌธ์„œ
    • ๋ชจ๋“ˆ
    • ๊ฐ์ฒด
    • ๋ฐฐ์—ด
    • ํ•จ์ˆ˜
    • ๋ฐ˜๋ณต๋ฌธ
    • ์กฐ๊ฑด๋ฌธ
    • ์ˆซ์ž์™€๋ฌธ์ž
    • ๋ณ€์ˆ˜
    • ๋น„๊ต
  • ๋…ธ๋งˆ๋“œ ์ฝ”๋”
    • Getting the Weather part One Geolocation
    • Image Background
    • TO DO List
    • Saving the User Name
    • Clock part One
    • ์กฐ๊ฑด๋ฌธ ( if , else, and, or)
    • evnet handlers
    • Function
    • Objects
    • Arrays
    • Variable(๋ณ€์ˆ˜!)
    • Javascript
  • javascript30
    • Dram Kit
    • clock
    • Css Javascript
    • Array Cardio
    • flex panels
    • type ahead
    • Canvas Draw
    • Speech Synthesis
    • Whack A Mole
  • web standard
    • script ๋ถ€๋ถ„
    • form๋ถ€๋ถ„
    • ์›น์ ‘๊ทผ์„ฑ
    • <meta>
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ_์ด๋ก 
    • ๊ธฐ๋ณธ์ง€์‹(JAVASCRIPT)
    • ๊ธฐ๋ณธ์ง€์‹(CSS)
    • ๊ธฐ๋ณธ์ง€์‹(HTML)
    • ๊ธฐ๋ณธ์ง€์‹(HTTP)
    • Dom
    • ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ํ”„๋ ˆ์ž„์›Œํฌ, ํ”Œ๋กœ๊ทธ์ธ
Powered by GitBook
On this page
  • fetch()
  • .push
  • findMatches()
  • ์‚ฌ์šฉ์ž ์ž…๋ ฅํ•œ ๋‹จ์–ด๋กœ ๊ฒฐ๊ณผ๊ฐ’ ์ฐพ๊ธฐ

Was this helpful?

  1. javascript30

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);
  <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>
html {
  box-sizing: border-box;
  background: #ffc600;
  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: #ffc600;
}

fetch()

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

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

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

fetch(`member.json`).then(function (blob) {
  blob.json()
    .then(function (data) {
      console.log(data);
      console.log('hello');
  });
})
[{
    "name": "boa",
    "age": "31"
  },
  {
    "name": "mori",
    "age": "26"
  },
  {
    "name": "hoon",
    "age": "39"
  },
  {
    "name": "haha",
    "age": "89"
  },
  {
    "name": "hoho",
    "age": "100"
  },
  {
    "name": "bobo",
    "age": "15"
  },
  {
    "name": "baba",
    "age": "2"
  }
]

.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๊ฐœ๊ฐ€ ํ•„์š”ํ•จ ํ•˜๋‚˜๋Š” ๊ฒ€์ƒ‰ํ•œ ๋‹จ์–ด, ํ•˜๋‚˜๋Š” ์ „์ฒด ๋ฐฐ์—ด

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

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

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

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);
}
Previousflex panelsNextCanvas Draw

Last updated 3 years ago

Was this helpful?

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

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

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

๐Ÿ‘‰
๐Ÿ‘‰
๐Ÿ‘‰