for in, for of index) ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ ์์๋ฅผ ์ฐธ์กฐ (0 ๋ถํฐ ์์)var score = new Array();var score = [];var score = new Array(90, 70, 100);var score = [ 90 , 70 , 100];score[3] = 86;score[3]arr[arr.length] = ๊ฐ vs arr.push(๊ฐ) ๋น๊ตํ
| ํญ๋ชฉ | arr[arr.length] = ๊ฐ | arr.push(๊ฐ) |
|---|---|---|
| ๊ธฐ๋ณธ ๊ฐ๋ | length๋ฅผ ์ฌ์ฉํด ๋ง์ง๋ง์ ์๋์ผ๋ก ๊ฐ ์ถ๊ฐ | ๋ง์ง๋ง์ ๊ฐ์ ์๋์ผ๋ก ์ถ๊ฐ |
| ์ง๊ด์ฑ | โ ๋ ์ง๊ด์ , ๊ฐ๋ ์ฑ ๋ฎ์ | โ ์ง๊ด์ ์ด๊ณ ๊ฐ๋ ์ฑ ์ข์ |
| ๋ค์ค ์ถ๊ฐ | โ ๋ถ๊ฐ๋ฅ (ํ๋์ฉ๋ง ๊ฐ๋ฅ) | โ
๊ฐ๋ฅ (arr.push(1, 2, 3)) |
| ์ฒด์ด๋ ๊ฐ๋ฅ ์ฌ๋ถ | โ ์์ | โ
๊ฐ๋ฅ (arr.push(1).push(2)) |
| ์ฌ์ฉ ์ถ์ฒ๋ | โ ๊ฐ๊ธ์ ์ง์ | โ ๋ฐฐ์ด ๋ง์ง๋ง์ ์ถ๊ฐํ ๋ ์ ๊ทน ์ฌ์ฉ |
| ์ฝ๋ ์ ์ง๋ณด์ | โ ์ธ๋ฑ์ค๋ฅผ ํ๋์ฝ๋ฉํ๋ฉด ์์ ์ด๋ ค์ | โ ์๋ ์ธ๋ฑ์ฑ์ผ๋ก ์ ์ง๋ณด์ ์ฌ์ |
gpt ๋ต๋ณ
๊ฒฐ๋ก ๋ถํฐ ๋งํ๋ฉด arr[arr.length] = ๊ฐ ์ arr.push(๊ฐ) ๋ ๊ฑฐ์ ๊ฐ์ ๋์์ ํด.
๋ ๋ค ๋ฐฐ์ด์ ๋ง์ง๋ง์ ๊ฐ์ ์ถ๊ฐํ๋ ๋ฐฉ์์ด์ผ.
ํ์ง๋ง ์ฝ๊ฐ์ ์ฐจ์ด์ ์ด ์กด์ฌํด! ์๋์ ์ ๋ฆฌํด์ค๊ฒ. ๐
| ํญ๋ชฉ | ์ค๋ช |
|---|---|
| ๋์ | ๋ฐฐ์ด์ ๋งจ ๋ง์ง๋ง ์ธ๋ฑ์ค์ ๊ฐ์ ์ถ๊ฐํจ |
| ๊ฒฐ๊ณผ | ๋ฐฐ์ด ๊ธธ์ด๊ฐ ์ฆ๊ฐํ๊ณ , ๋ง์ง๋ง์ ๊ฐ์ด ์ถ๊ฐ๋จ |
์:
let arr = [1, 2, 3];
arr[arr.length] = 500; // arr์ [1, 2, 3, 500]
arr.push(600); // arr์ [1, 2, 3, 500, 600]
โ ๏ธ ์ฐจ์ด์
| ๊ตฌ๋ถ | arr.push(value) | arr[arr.length] = value |
|---|---|---|
| ์ง๊ด์ฑ | โ ๋ ์ง๊ด์ ์ด๊ณ ๋ช ํ | โ ๋ ์ง๊ด์ |
| ๋ฉ์๋ ํธ์ถ | push()๋ ํจ์ ํธ์ถ | ๋๊ดํธ๋ฅผ ์ด์ฉํ ์ง์ ํ ๋น |
| ๋ค์ค ์ถ๊ฐ | ์ฌ๋ฌ ๊ฐ์ ๋์์ ์ถ๊ฐ ๊ฐ๋ฅ arr.push(1, 2) | โ ํ ๋ฒ์ ํ๋๋ง ์ถ๊ฐ ๊ฐ๋ฅ |
Array.prototype ํจ์ | push๋ JS ๋ด๋ถ ๋ฉ์๋๋ผ ์ฒด์ด๋ ๊ฐ๋ฅ | ๋ถ๊ฐ๋ฅ |
โ ์์ ๋น๊ต
let a = [1, 2];
a.push(3, 4); // ๊ฐ๋ฅ
console.log(a); // [1, 2, 3, 4]
a[a.length] = 5; // ๊ฐ๋ฅ
console.log(a); // [1, 2, 3, 4, 5]
a[a.length] = 6, 7; // ์ด๋ ๊ฒ ํ๋ฉด 6๋ง ๋ค์ด๊ฐ (์ฃผ์)
console.log(a); // [1, 2, 3, 4, 5, 6]
๐ ์ค๋ฌด์์๋?
push() ๋ฅผ ๋ ๋ง์ด ์จ!์ ๋ฆฌ
index ๋ฅผ ํตํ ์ ๊ทผ : ๋ฐฐ์ด์ฒ๋ผ index ๋ฅผ ์ฌ์ฉํด์ ๊ฐ ์์์ ์ ๊ทผ ๊ฐ๋ฅlength ์์ฑ : ๋ฐฐ์ด์ฒ๋ผ length ์์ฑ์ด ์์ด์ for ๊ตฌ๋ฌธ์ ์ด์ฉํ๋ฉด ๋ชจ๋ ์์ ์ฐธ์กฐ ๊ฐ๋ฅ๋ฐฐ์ด ๋ฉ์๋ ์์ : forEach, map ๋ฑ์ ๋ฐฐ์ด ๋ฉ์๋๊ฐ ์์์์
const arr = {
length: 3,
0: "orange",
1: "yellow",
2: "green",
};
printArr(arr);
| ๊ตฌ๋ถ โ k โจ i | [0] | [1] | [2] | [3] |
|---|---|---|---|---|
| [0] | 2 | 4 | 6 | 8 |
| [1] | 3 | 6 | 9 | 12 |
| [2] | 4 | 8 | 12 | 16 |
| [3] | 5 | 10 | 15 | 20 |
var numbers = [
[2, 4, 6, 8 ],
[3, 6, 9, 12],
[4, 8, 12, 36],
[5, 10, 15, 20]
];
// 1์ฐจ์ ๋ฐฐ์ด ์ถ๋ ฅํ๊ธฐ
console.log(numbers[0]); // [2, 4, 6, 8 ]
console.log(numbers[1]); // [3, 6, 9, 12]
console.log(numbers[2]); // [4, 8, 12, 36]
console.log(numbers[3]); // [5, 10, 15, 20]
// 2์ฐจ์ ๋ฐฐ์ด ์ถ๋ ฅํ๊ธฐ
console.log(numbers[0][0]); // 2
console.log(numbers[0][1]); // 4
console.log(numbers[1][0]); // 3
console.log(numbers[3][2]); // 15
for in, for offor of ๊ด๋ จ ๋ฌธ์
for...ofย ๋ช
๋ น๋ฌธ์ย ๋ฐ๋ณต๊ฐ๋ฅํ ๊ฐ์ฒดย (Array,ย Map,ย Set, String,ย TypedArray,ย argumentsย ๊ฐ์ฒด ๋ฑ์ ํฌํจ)์ ๋ํด์ ๋ฐ๋ณตํ๊ณ ๊ฐ ๊ฐ๋ณ ์์ฑ๊ฐ์ ๋ํด ์คํ๋๋ ๋ฌธ์ด ์๋ ์ฌ์ฉ์ ์ ์ ๋ฐ๋ณต ํํฌ๋ฅผ ํธ์ถํ๋ ๋ฃจํ๋ฅผ ์์ฑํฉ๋๋ค.
// ๊ฐ์ฒด ์์ฑ
const array1 = ["a", "b", "c"];
// for of
for (const element of array1) {
console.log(element);
}
/*
์ถ๋ ฅ ๊ฐ
a, b, c
*/
for in ๊ด๋ จ๋ฌธ์
for...inย ๋ฌธ์ ์์๋ ์ด๊ฑฐ ๊ฐ๋ฅํ ์์ฑ๋ค์ ํฌํจํ์ฌ ๊ฐ์ฒด์์ ๋ฌธ์์ด๋ก ํค๊ฐ ์ง์ ๋ ๋ชจ๋ ์ด๊ฑฐ ๊ฐ๋ฅํ ์์ฑ์ ๋ํด ๋ฐ๋ณตํฉ๋๋ค. (Symbol๋ก ํค๊ฐ ์ง์ ๋ ์์ฑ์ ๋ฌด์ํฉ๋๋ค.)
// ๊ฐ์ฒด ์์ฑ
const object = { a: 1, b: 2, c: 3 };
for (const property in object) {
console.log(`${property}: ${object[property]}`);
}
/*
์ถ๋ ฅ ๊ฐ
"a: 1"
"b: 2"
"c: 3"
*/
๐ for...in vs for...of
ํ์ค์์ฝ: for...in์ ๊ฐ์ฒด์ ์ด๋ฆํ(ํค)๋ฅผ ํ๋์ฉ ๊บผ๋ผ ๋ ์ฐ๊ณ , for...of๋ ๋ฐฐ์ด์ด๋ ๊ธ์์ฒ๋ผ ํ๋์ฉ ๊บผ๋ผ ์ ์๋ ๊ฒ์ ๋ด์ฉ(๊ฐ)์ ํ๋์ฉ ๊บผ๋ผ ๋ ์จ! โ
| ๊ตฌ๋ถ | for...in | for...of |
|---|---|---|
| ๐ ์ฉ๋ | ๊ฐ์ฒด์ ํค(key)๋ฅผ ๋ฐ๋ณต | ๋ฐฐ์ด, ๋ฌธ์์ด ๋ฑ ์ดํฐ๋ฌ๋ธ(iterable)์ ๊ฐ(value)์ ๋ฐ๋ณต |
| ๐ ๋ฐ๋ณต ๋์ | ๊ฐ์ฒด, ๋ฐฐ์ด (ํค ๊ธฐ์ค) | ๋ฐฐ์ด, ๋ฌธ์์ด, Map, Set ๋ฑ (๊ฐ ๊ธฐ์ค) |
| ๐ ๋ฐํ๊ฐ | ์์ฑ ์ด๋ฆ(๋ฌธ์์ด) ๋๋ ์ธ๋ฑ์ค | ๊ฐ(value) |
| โ ๏ธ ์ฃผ์์ฌํญ | ๋ฐฐ์ด ์ํ ์ ์ธ๋ฑ์ค๋ฅผ ๋ฌธ์์ด๋ก ๋ฐํํ๋ฏ๋ก ์ฃผ์ ํ์ | ๊ฐ์ฒด์๋ ์ฌ์ฉํ ์ ์์ (์ค๋ฅ ๋ฐ์) |
๐ก ์ธ์ ์ด๋ค ๊ฑธ ์จ์ผ ํ ๊น?
| ์ํฉ | ์ฌ์ฉ |
| --- | --- |
| ๊ฐ์ฒด์ key๋ฅผ ํ๋์ฉ ์ํํด์ผ ํ๋ค | for...in |
| ๋ฐฐ์ด์ด๋ ๋ฌธ์์ด์ ๊ฐ๋ง ํ์ํ๋ค | for...of |