| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
- textshadow
- ํฅ์ค์ฝ๋
- bordercollapse
- fontweight
- CDN
- ํผํ๊ทธ
- wordspacing
- captionside
- ๊ทธ๋ฃน์ ๋ ํฐ
- ๋ชจ๋์นํฐํธ
- liststyleposition
- formtag
- liststyletype
- textdecoration
- ํฐํธ์จ์ดํธ
- selectorslevel4
- borderspacing
- texttransform
- ์ถ์ฝ
- ์์ด๋์ ๋ ํฐ
- ํด๋์ค์ ๋ ํฐ
- ํ์ ์ ๋ ํฐ
- ์ฃผ์
- ๋นํ๊ทธ
- gtammertable
- ์ ๋๋ฒ์ ์ ๋ ํฐ
- ๊ตฌ๊ธํฐํธ
- borderstyle
- liststyleimage
- inlineblock
- Today
- Total
็ก์
Array Iteration (๋ฐฐ์ด ๋ฐ๋ณต) ๋ณธ๋ฌธ
๐จ Array Iteration
๋ฐฐ์ด(Array)์ ๋ชจ๋ ์์๋ฅผ ํ๋์ฉ ๊บผ๋ด์ด ์์๋๋ก ์ฒ๋ฆฌํ ๋ ์ฌ์ฉํ๋ ๋ฐ๋ณต ๋ฐฉ์์ด๋ค.
๐ฃ for ๋ฌธ (์ ํต ๋ฐฉ์)
์ธ๋ฑ์ค๋ฅผ ์ง์ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ์ํํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ.
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
- ํน์ง: ์ธ๋ฑ์ค(i)๋ฅผ ์ง์ ๊ด๋ฆฌํ๋ฉฐ, ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ด๊ณ ๋ชจ๋ ์ํฉ์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
- ์ฅ์ : break, continue ๋ชจ๋ ๊ฐ๋ฅํ๋ค.
- ๊ตฌ์กฐ:
- arr.length: ๋ฐฐ์ด์ ์ ์ฒด ๊ธธ์ด(์์ ๊ฐ์)๋ฅผ ๋ฐํํ๋ค.
- arr[i]: ํ์ฌ ์ธ๋ฑ์ค์ ํด๋นํ๋ ์์๋ฅผ ๊ฐ์ ธ์จ๋ค.
- i++: ๋ค์ ์์๋ก ์ด๋ํ๋ค.
๐ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ์ง์ ์ซ์๋ก ์์ฑํ์ง ์๊ณ length๋ฅผ ์ฌ์ฉํ๋ฉด ์์๊ฐ ์ถ๊ฐ๋๊ฑฐ๋ ์ญ์ ๋์ด๋ ์ฝ๋๋ฅผ ์์ ํ ํ์๊ฐ ์๋ค.
๐ฃ for...of ๋ฌธ (๊ฐ์ฅ ๊น๋ํ ๋ฐ๋ณต๋ฌธ)
๋ฐฐ์ด์ ๊ฐ์ ์ง์ ๊บผ๋ด์ ๋ฐ๋ณตํ๋ ๋ฐฉ์.
for (const item of arr) {
console.log(item);
}
- ํน์ง: ์ธ๋ฑ์ค(i)๋ฅผ ์ง์ ๋ค๋ฃจ์ง ์๊ณ ๊ฐ ์์ฒด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ๋๋ฌธ์ ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํ๋ค.
- ์ฅ์ : break, continue, return ๋ฑ์ ์ฌ์ฉํ์ฌ ๋ฐ๋ณต์ ์ ์ดํ ์ ์๋ค.
๐ฃ forEach()
๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ง์ ํ ์ฝ๋ฐฑ ํจ์๋ฅผ ์คํํ๋, ๋ฐฐ์ด ์ ์ฉ ๋ฐ๋ณต ๋ฉ์๋.
arr.forEach((item, index) => {
console.log(item, index);
});
- ํน์ง: ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ฒ์๋ถํฐ ๋๊น์ง ์์๋๋ก ์ํํ๋ค.
- ๊ตฌ์ฑ: ์ฝ๋ฐฑ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ์ฒซ ๋ฒ์งธ๋ ๊ฐ(item), ๋ ๋ฒ์งธ๋ ์ธ๋ฑ์ค(i)๋ฅผ ๋ฐ์ ์ ์๋ค.
- ์ฃผ์: break, continue๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ํญ์ ๋๊น์ง ๋ชจ๋ ์์๋ฅผ ์ํํ๋ฏ๋ก ์ค๊ฐ์ ๋ฉ์ถ ์ ์๋ค.
๐ POINT ๐
โ๏ธ ๋จ์ํ ๋ฐฐ์ด ๊ฐ์ ์ฝ์ ๋๋ for...of๊ฐ ๊ฐ์ฅ ๊น๋ํ๋ค. โ๏ธ
โ๏ธ ๊ฐ + ์ธ๋ฑ์ค๊ฐ ํ์ํ๋ฉด forEach๊ฐ ์ ์ฉํ๋ค. โ๏ธ
โ๏ธ ํ๋ JS์์๋ for...of, forEach๋ฅผ ๋ง์ด ์ฌ์ฉํ๋ค. โ๏ธ
๐ ์ ํต์ for vs for...of vs forEach ๐
| ๊ตฌ๋ถ | ์ผ๋ฐ for | for...of | forEach |
| ๊ฐ๋ ์ฑ | ๋ฎ์ | ๋์ | ๋์ |
| ์ธ๋ฑ์ค ์ ๊ทผ | ์ง์ ๊ด๋ฆฌ | ์ด๋ ค์ | ์ฌ์ |
| ์ค๋จ ๊ฐ๋ฅ์ฑ | ๊ฐ๋ฅ | ๊ฐ๋ฅ | ๋ถ๊ฐ๋ฅ |
| ์ฌ์ฉ ๋์ | ๋ชจ๋ ๋ฐ๋ณต | ๋ฐ๋ณต ๊ฐ๋ฅํ ๊ฐ์ฒด | ๋ฐฐ์ด |
โ๏ธ ์ค์ ์์ ์ฝ๋ โ๏ธ
const scores = [85, 42, 98, 60, 77];
const passingScores = [];
// ์ ์ ๋ชฉ๋ก์์ ํ๋์ฉ ๊บผ๋ด์ด ๊ฒ์ฌํฉ๋๋ค.
for (const score of scores) {
if (score >= 70) {
passingScores.push(score);
}
}
console.log("ํฉ๊ฒฉ์ ์ ์ ๋ชฉ๋ก:", passingScores); // [85, 98, 77]
// forEach๋ฅผ ์ฌ์ฉํ ํฉ๊ณ ๊ณ์ฐ
let total = 0;
scores.forEach(s => total += s);
console.log("ํ๊ท ์ ์:", total / scores.length);
'<script>' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| break์ continue (0) | 2026.05.28 |
|---|---|
| Array Transformation (๋ฐ์ดํฐ ๋ณํ) (0) | 2026.05.28 |
| Loop statement (๋ฐ๋ณต๋ฌธ) (0) | 2026.05.28 |
| Truthy and Falsy (์ฐธ ๊ฐ์ ๊ฐ & ๊ฑฐ์ง ๊ฐ์ ๊ฐ) (0) | 2026.05.28 |
| Conditional statement (์กฐ๊ฑด๋ฌธ) (0) | 2026.05.28 |
