[TIL] JavaScript | for, for-of, for-in, forEach, map

dosilvยท2021๋…„ 4์›” 15์ผ
0
post-thumbnail
post-custom-banner

๐ŸŽฒ ๋ชฉํ‘œ: ํŒŒ์ด์ฌ์˜ for๋ฌธ๊ณผ ํ—ท๊ฐˆ๋ฆฌ๋Š”๐Ÿ™„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‹ค์–‘ํ•œ for๋ฌธ(+๋ฉ”์„œ๋“œ) ์ดํ•ดํ•˜๊ธฐ

๋ฉฐ์น  ๊ณ ๋ฏผํ•˜๋‹ค ์–ด์ œ ๊ฐ‘์ž‘์Šค๋ ˆ ํ”„๋ก ํŠธ๋กœ ๋ถ„์•ผ๋ฅผ ๋ฐ”๊ฟจ๋‹ค! ๋งŒ๋“œ๋Š” ๋Œ€๋กœ ๋ˆˆ์— ๋ณด์ด๋Š” ๊ฒŒ ๋„ˆ๋ฌด ์žฌ๋ฐŒ์–ด...... ๊ทผ๋ฐ ์ž๊พธ ๋ฌธ์ œ ํ’€๋ฉด์„œ ํŒŒ์ด์ฌ ๋ฌธ๋ฒ•์ด ํŠ€์–ด๋‚˜์˜ด๐Ÿ˜… ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋” ์—ด์‹ฌํžˆ ๋”ฐ๋ผ๊ฐ€์•ผ์ง• ๐Ÿ’ช๐Ÿ’ช


โ–ถ ๋ชฉ์ฐจ
๐Ÿ”Ž for
๐Ÿ”Ž for-of
๐Ÿ”Ž for-in
๐Ÿ”Ž forEach
๐Ÿ”Ž map


๐Ÿ”Ž for

ํŒŒ์ด์ฌ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋ฐ˜๋ณต์ˆ˜ํ–‰์ด ํ•„์š”ํ•  ๋•Œ for๋ฌธ์„ ์“ด๋‹ค. ๊ธฐ๋ณธ์ ์ธ for๋ฌธ์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

for (let i; i < 10; i++) {
  ์‹คํ–‰ํ•  ํ•จ์ˆ˜
  }

๊ด„ํ˜ธ ์•ˆ์˜ ๋‚ด์šฉ์€ (๋ณ€์ˆ˜; ๋ฐ˜๋ณต์„ ๋๋‚ผ ๋ณ€์ˆ˜์˜ ์กฐ๊ฑด; for๋ฌธ์ด ํ•œ ๋ฒˆ ๋๋‚  ๋•Œ๋งˆ๋‹ค ๋ณ€์ˆ˜์˜ ๋ณ€ํ™”)๋กœ ๋ณด๋ฉด ๋จ!

for๋ฌธ์„ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด(Array)์˜ ์š”์†Œ๋ฅผ ๋ฐ˜๋ณต ํ˜ธ์ถœํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const fruits = ["lemon", "melon", "blueberry", "peach"];
for (let i; i < fruits.length; i++) {
  console.log(fruits[i]);
  }

๐Ÿ”Ž for-of

for-of๋Š” ES6์—์„œ ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ•์œผ๋กœ์„œ, ์ผ๋ฐ˜ for๋ฌธ๋ณด๋‹ค ํŽธ๋ฆฌํ•˜๊ฒŒ Array, String, Map, Set ๋“ฑ์„ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค.

const fruits = ["lemon", "melon", "blueberry", "peach"];
for (let i of fruits) {
  console.log(i);
  }

/*์‹คํ–‰๊ฒฐ๊ณผ:
  lemon
  melon
  blueberry
  peach*/
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋Š” ์กฐ๊ฑด์„ ์ง์ ‘ ์„ค์ •ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ , i๊ฐ€ ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ i๋ฒˆ์งธ ์š”์†Œ ๊ทธ ์ž์ฒด๋กœ ํ• ๋‹น๋˜๊ธฐ ๋•Œ๋ฌธ์— ํŽธ๋ฆฌํ•จ
  • continue์™€ break๋กœ ๋ฐ˜๋ณต์„ ์Šคํ‚ตํ•˜๊ฑฐ๋‚˜ ์ค‘๋‹จํ•  ์ˆ˜ ์žˆ์Œ!
  • Object์—๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ (์ˆœ์„œ๊ฐ€ ์žˆ๋Š” iterableํ•œ ์ž๋ฃŒํ˜•์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ)

๐Ÿ”Ž for-in

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ES6์—์„œ ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ•. for-of์™€ ๋‹ฌ๋ฆฌ Object์—๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ!

const colorfulfruits = {
  yellow: "lemon",
  green: "melon",
  purple: "blueberry",
  pink: "peach"
  };
for (let i in colorfulfruits) {
  console.log(i, colorfulfruits[i]);
  }

/*์‹คํ–‰๊ฒฐ๊ณผ:
  yellow lemon
  green melon
  purple blueberry
  pink peach*/

์œ„์—์„œ ์•Œ ์ˆ˜ ์žˆ๋“ฏ์ด i๋Š” object์˜ key๊ฐ’์ด ๋˜๊ณ , object[i]๋กœ value๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ Array์— for-in์„ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ

const fruits = ["lemon", "melon", "blueberry", "peach"];
for (let i of fruits) {
  console.log(i);
  }

/*์‹คํ–‰๊ฒฐ๊ณผ:
  0
  1
  2
  3*/

i๋Š” ๊ฐ ์š”์†Œ๊ฐ€ ์•„๋‹Œ ์ธ๋ฑ์Šค๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•˜์ž!


๐Ÿ”Ž forEach

forEach๋Š” Array์˜ ๋ฉ”์„œ๋“œ๋กœ์„œ ๊ฐ ์š”์†Œ๋ฅผ ์ธ์ž๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

const fruits = ["lemon", "melon", "blueberry", "peach"];
fruits.forEach(i => console.log(`I like ${}`));

/*์‹คํ–‰๊ฒฐ๊ณผ:
  I like lemon
  I like melon
  I like blueberry
  I like peach*/

๋งŒ์•ฝ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ž๋ฅผ ์„ธ ๊ฐœ ์„ค์ •ํ•  ๊ฒฝ์šฐ ๊ฐ๊ฐ ์š”์†Œ, ์ธ๋ฑ์Šค, ์ „์ฒด ๋ฐฐ์—ด์ด ๋œ๋‹ค.

const fruits = ["lemon", "melon", "blueberry", "peach"];
fruits.forEach((a, b, c) => console.log(a, b, c));

/*์‹คํ–‰๊ฒฐ๊ณผ:
  lemon 0 4
  melon 1 4
  blueberry 2 4
  peach 3 4*/

๐Ÿ”Ž map

map ๋˜ํ•œ Array์˜ ๋ฉ”์„œ๋“œ๋กœ, forEach์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๊ฐ ์š”์†Œ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์‹คํ–‰ ๊ฒฐ๊ด๊ฐ’๋“ค๋กœ ์ƒˆ Array๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ํŒŒ์ด์ฌ์˜ map๊ณผ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๊ฒƒ ๊ฐ™๋‹ค. forEach๋Š” for๋ฌธ์ฒ˜๋Ÿผ ํ•จ์ˆ˜์˜ ๋ฐ˜๋ณต์‹คํ–‰๋งŒ ํ•  ๋ฟ, ๋ฆฌํ„ด๊ฐ’์ด ์—†์Œ!

const fruits = ["lemon", "melon", "blueberry", "peach"];
const re = fruits.map(i => `fresh ${i}`);
console.log(re);
/*์‹คํ–‰๊ฒฐ๊ณผ:
  ["fresh lemon", "fresh melon", "fresh blueberry", "fresh peach"]*/

array.map((x, y, z) => {} ) ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ธ์ž ์„ธ ๊ฐœ๋ฅผ ๋ฐ›์„ ๊ฒฝ์šฐ ๊ฐ๊ฐ ์š”์†Œ, ์ธ๋ฑ์Šค, ๋ฐฐ์—ด์ด ํ• ๋‹น๋จ!




๐Ÿ™‡โ€โ™€๏ธ์ฐธ๊ณ ํ•œ ์ž๋ฃŒ

[Javascript] forEach, some, for in, for of (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, ๋ฐ˜๋ณต, ๋ฐฐ์—ด ๋ฐ˜๋ณต, ํ”„๋กœํ† ํƒ€์ž…, prototype, ๊ฐ์ฒด ๋ฐ˜๋ณต)
Array.prototype.forEach() - JavaScript | MDN

profile
DevelOpErUN ์„ฑ์žฅ์ผ๊ธฐ๐ŸŒˆ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€