๐Ÿ™†๐Ÿปโ€โ™€๏ธ [JS] ์ด์ค‘ for๋ฌธ break ๐Ÿ™†๐Ÿปโ€โ™€๏ธ

์ด์ค‘ for๋ฌธ ๋น ์ ธ ๋‚˜์˜ค๊ธฐ!

์ •ํ•ด์ง„ ๋ฒ”์œ„ ๋งŒํผ tr์•ˆ์˜ ๋ชจ๋“  td๋ฅผ ๋Œ๋ฉด์„œ ์ž‘์—…์„ ํ•ด์ค˜์•ผ ํ–ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํŠน์ • ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋ฉด for๋ฌธ์ด ์ข…๋ฃŒ ๋์–ด์•ผ ํ–ˆ๋Š”๋ฐ, ์›ํ•˜๋Š”๋Œ€๋กœ ๋˜์ง€ ์•Š์•˜๋‹ค.

  • ์ฒ˜์Œ ๊ตฌํ˜„ํ•œ ๊ฒƒ
for (let i = begin.idx; i <= end.idx; i++) {
  tdList.forEach(el => (
    if (ํŠน์ • ์กฐ๊ฑด) {
      return false;
    }
    // do something   
  ))
}

ํ—คํ—ท ๋จธ์“ฑ๋จธ์“ฑ...^^ forEach๋ฌธ์ด ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ํ•ด์„œ for๋ฌธ์ด ์ข…๋ฃŒ๋˜์ง„ ์•Š๋Š”๋‹ค๊ตฌ์›ƒ~!
๊ทธ๋ž˜์„œ forEach๋ฌธ์„ for๋ฌธ์œผ๋กœ ์ˆ˜์ •ํ•˜์—ฌ ์ด์ค‘ for๋ฌธ์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

  • ๋‘ ๋ฒˆ์งธ ๋„์ „
for (let i = begin.idx; i <= end.idx; i++) {
  for (let j = 0; j < tdList.length; j++) {
  	if (ํŠน์ • ์กฐ๊ฑด) {
      break;
    }
    // do somthing
  }
}

ํ•˜์ง€๋งŒ ๋˜ ์›ํ•˜๋Š”๋Œ€๋กœ ๊ตฌํ˜„์ด ๋˜์ง€ ์•Š์•˜๋‹ค.
์•ˆ์ชฝ์˜ for๋ฌธ์ด ์ข…๋ฃŒ๋œ๋‹ค๊ณ  ํ•ด์„œ ๋ฐ”๊นฅ์ชฝ์ด ์ข…๋ฃŒ๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ๊ตฌ์›ƒ~^^

for๋ฌธ label

๊ทธ๋ ‡๋‹ค๋ฉด ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€?
๋ฐ”๋กœ label์„ ๋ถ™์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

outerLoop: for (let i = begin.idx; i <= end.idx; i++) {
  for (let j = 0; j < tdList.length; j++) {
  	if (ํŠน์ • ์กฐ๊ฑด) {
      break outerLoop;
    }
    // do somthing
  }
}

label ๋ช…์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์‹๋ณ„์ž๋ฉด ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค.
(๋‹จ, strict mode์—์„œ๋Š” 'let'์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.)
label๋Š” continue ํ•˜๊ณ  ์‹ถ์€ ์ƒํ™ฉ์—์„œ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

์–ํ˜ธ! ์„ฑ๊ณต์ด๋‹คโœจ

for๋ฌธ label

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ