Zoeeey
Stack flows in you
Zoeeey
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (155)
    • ๐Ÿ’ก UXUI (4)
      • UXUI (2)
      • UI (2)
      • UX (0)
    • ๐ŸŸจ JavaScript (66)
      • ๊ฐœ๋… (42)
      • ์ •๋ฆฌ (2)
      • ๋ฌธ์ œํ’€๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ์ฝ”๋”ฉ์• ํ”Œ) (15)
      • ์‘์šฉํ•˜์—ฌ ์ž๋™ํ™”or๋ฌธ์ œํ•ด๊ฒฐํ•˜๊ธฐ (2)
      • ๊ธฐํƒ€ (5)
    • โš›๏ธ React (49)
      • ๊ฐœ๋… (27)
      • ๊ฐœ๋…(Redux) (3)
      • ์ •๋ฆฌ (4)
      • ๋ฌธ์ œํ’€๊ธฐ (์ฝ”๋”ฉ์• ํ”Œ: React) (15)
    • ๐Ÿ“˜ TypeScript (10)
      • ๊ฐœ๋… (8)
      • ๋ฌธ์ œํ’€๊ธฐ (์ฝ”๋”ฉ์• ํ”Œ) (2)
    • ๐ŸŒ HTML,CSS (18)
      • ์‹ค๋ฌด์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋“ค (10)
      • ๋’ค๋Šฆ๊ฒŒ ์•Œ์•„์„œ ๋ผˆ์•„ํ”ˆ ์œ ์šฉํ•œ CSS (6)
      • ๊ธฐํƒ€ ์ž์ฃผ ์“ฐ๋Š” CSS (2)
    • ๐Ÿ’ป CS (8)
      • CS (5)
      • ์ •๊ทœํ‘œํ˜„์‹ (2)
      • Git,Github (1)

ํƒœ๊ทธ

  • useEffect
  • redux
  • ์ฝ”๋”ฉ์• ํ”Œ
  • axios
  • UXUI
  • json
  • TypeScript
  • object
  • array
  • CS
  • Props
  • state
  • react
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • CSS
  • class
  • ์˜ค๋ฅ˜
  • Ajax
  • ์‚ผํ•ญ์—ฐ์‚ฐ์ž
  • JavaScript
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
hELLO ยท Designed By ์ •์ƒ์šฐ.
Zoeeey

Stack flows in you

๐ŸŸจ JavaScript/๋ฌธ์ œํ’€๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ์ฝ”๋”ฉ์• ํ”Œ)

Level 3 : 4 (array, object, forEach)

2023. 1. 31. 15:44

4. ๋ฐ”์ง€ ์„ ํƒํ•˜๋ฉด array์— ์ €์žฅํ•œ ๋ฐ”์ง€์‚ฌ์ด์ฆˆ ์…€๋ ‰๋ฐ•์Šค ๋งŒ๋“ค๊ธฐ

์กฐ๊ฑด

๋ฐ”์ง€๋ฅผ ์„ ํƒํ•˜๋ฉด array์— ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ์ด์ฆˆ ์…€๋ ‰๋ฐ•์Šค์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•œ๋‹ค. 


ํ‹€๋ฆฐ ํ’€์ด

<form class="container my-5 form-group">
  <p>์ƒํ’ˆ ์„ ํƒ</p>
  <select class="form-select mt-2">
    <option>๋ชจ์ž</option>
    <option>์…”์ธ </option>
    <option>๋ฐ”์ง€</option>
  </select>
  <select class="form-select form-popup mt-2 form-hide">
    <option>100</option>
  </select>
</form>
let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function pantsSize() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');

        for (i = 0; selectSize.length = pants.length; i++) {
            document.querySelectorAll('.form-popup option')[i].innerHTML = pants[i];
            selectSize.appendChild(document.createElement('option'));
        }
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', pantsSize);

์ˆ˜์น˜์‚ฌํ• ๊นŒ ๋ด ์˜ฌ๋ฆด๊นŒ ๋ง๊นŒ ๊ณ ๋ฏผ ๋งŽ์ด ํ–ˆ๋Š”๋ฐ.. for๋ฌธ ์กฐ๊ฑด์‹์„ ๋ถ€๋“ฑํ˜ธ๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋นˆ ์…€๋ ‰๋ฐ•์Šค๋กœ ๋…ธ์ถœ๋˜์–ด ๋“ฑํ˜ธ๋กœ ๋ฐ”๊ฟจ๋”๋‹ˆ ์ž‘๋™์ด ๋˜์–ด๋ฒ„๋ ค์„œ ๋“ฑํ˜ธ๋กœ ํ–ˆ๋‹ค. ์ด๋Š” ๋‹น์—ฐํ•œ ๊ฑด๋ฐ, ๋“ฑํ˜ธ๊ฐ€ ์„ ์–ธ์ฒ˜๋Ÿผ ์ž‘๋™ํ•˜์—ฌ ๋ฌดํ•œ๋ฃจํ”„ ์กฐ๊ฑด์‹์ด ๋˜์–ด๋ฒ„๋ฆฐ ๊ฒƒ.


์ž‘๋™ํ•˜๋Š” ํ’€์ด

let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function pantsSize() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');

        for (i = selectSize.length; i < pants.length; i++) {
            selectSize.appendChild(document.createElement('option'));
            document.querySelectorAll('.form-popup option')[i].innerHTML = pants[i];
        }
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', pantsSize);

์˜๋ฌธ์ด์—ˆ๋˜ ๋ถ€๋ถ„

์™œ์ธ์ง€ let selectSizeElement = document.querySelectorAll('.form-popup option'); ์œผ๋กœ option์„ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•ด ๋‘๊ณ  for๋ฌธ์•ˆ์—  document.querySelectorAll('.form-popup option')[i]์„ selectSizeElement[i]์œผ๋กœ ์ฃผ๋‹ˆ selectSizeElement[i]๊ฐ€ undefined๊ฐ€ ๋‚˜์™”๋‹ค. ๋””๋ฒ„๊น… ์ง์ ‘ document.querySelectorAll('.form-popup option')[i]์œผ๋กœ ์ฝ˜์†”์— ์ฐ์–ด๋ณด๋ฉด ๊ฐ’์ด ์ œ๋Œ€๋กœ ๋‚˜์™”๋‹ค. ๋ณ€์ˆ˜ ์ˆ˜์— ํ• ๋‹นํ•œ ๊ฐ’์„ ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ธ๋ฑ์Šค๋ฅผ ์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”๊ฑธ๊นŒ?

์˜๋ฌธ ํ•ด๊ฒฐ

document.querySelectorAll('.form-popup option')์„ ์ „์—ญ๋ณ€์ˆ˜๋กœ ์„ค์ •ํ•˜๋‹ˆ(if๋ฌธ ์ฒซ์ค„์— ๋„ฃ์–ด๋„ ๋˜‘๊ฐ™๋‹ค) createElement๋กœ ์ƒˆ๋กœ์šด option์„ ๋งŒ๋“ค๊ธฐ ์ „์— ์ฝ์–ด๋ฒ„๋ ค์„œ ์ธ๋ฑ์Šค๋“ค์ด ์ฝ์–ด์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

for (i = selectSize.length; i < pants.length; i++) {
    selectSize.appendChild(document.createElement('option'));
    let selectSizeElement = document.querySelectorAll('.form-popup option'); //์—ฌ๊ธฐ
    selectSizeElement[i].innerHTML = pants[i];
}

(๊ทธ๋ž˜์„œ ์ด๊ฑด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค)


forEach ๋ฒ„์ „

let pants = [28, 30, 32];

let selectBox = document.querySelector('.form-select');
let selectSize = document.querySelector('.form-popup');

function selectSizePop() {
    if (selectBox.value == '๋ฐ”์ง€') {
        selectSize.classList.remove('form-hide');
        selectSize.innerHTML = '';
        
        pants.forEach(function(pantsSize){
            selectSize.insertAdjacentHTML('beforeend', `<option>${pantsSize}</option>`);
        });
    } else { selectSize.classList.add('form-hide'); }
}
selectBox.addEventListener('input', selectSizePop);

์ถœ์ฒ˜ : ์ฝ”๋”ฉ์• ํ”Œ https://codingapple.com/

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋™์ผ์กฐ๊ฑด

'๐ŸŸจ JavaScript > ๋ฌธ์ œํ’€๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ์ฝ”๋”ฉ์• ํ”Œ)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Level 3 : 9 ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฒ„ํŠผ ๊ตฌํ˜„ํ•˜๊ธฐ(localStorage, array, forEach, JSON)  (0) 2023.04.15
Level 3 : 5~8 (ajax, json, array, sort)  (0) 2023.04.13
Level 3 : 1~3 (array, object)  (0) 2023.01.31
[์ค‘์š”] Level 2 : 14,15(์ˆ™์ œ ๋ฐ ๊นจ๋‹ฌ์€ ์ )_for, ๋ฒ„๋ธ”๋งํ™œ์šฉ์ถ•์•ฝ๋ฒ„์ „  (0) 2023.01.13
Level 2 : 6~13(์ˆ™์ œ ๋ฐ ๊นจ๋‹ฌ์€ ์ )_scroll  (0) 2023.01.12
    '๐ŸŸจ JavaScript/๋ฌธ์ œํ’€๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ์ฝ”๋”ฉ์• ํ”Œ)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • Level 3 : 9 ์žฅ๋ฐ”๊ตฌ๋‹ˆ๋ฒ„ํŠผ ๊ตฌํ˜„ํ•˜๊ธฐ(localStorage, array, forEach, JSON)
    • Level 3 : 5~8 (ajax, json, array, sort)
    • Level 3 : 1~3 (array, object)
    • [์ค‘์š”] Level 2 : 14,15(์ˆ™์ œ ๋ฐ ๊นจ๋‹ฌ์€ ์ )_for, ๋ฒ„๋ธ”๋งํ™œ์šฉ์ถ•์•ฝ๋ฒ„์ „
    Zoeeey
    Zoeeey
    ๊ธธ๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ์งง๊ฒŒ ๊ทธ๋ฆฌ๊ธฐ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”