5. Indexed collection
Array(๋ฐฐ์ด)
์์ฑ
// ๋ฐฐ์ด ๊ฐ์ฒด ์์ฑ
let arr1 = ['a', 1];
console.log(arr1); // ['a', 1]
let arr2 = [];
console.log(arr2); // []
arr2 = ['b', 2];
console.log(arr2); // ['b', 2]
// new Array
const arr3 = new Array(3);
console.log(arr3); // [ <3 empty items> ]
// Array.of
const arr4 = Array.of(1, 2, 3);
console.log(arr4); // [ 1, 2, 3 ]
// Array.from
const arr5 = Array.from(arr4);
console.log(arr5); // [1, 2, 3];
console.log(arr4 === arr5); // false
// ์ ์ฌ๋ฐฐ์ด๋ก ์์ฑ
// ํค์ ๊ฐ์ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ก, ์ฌ๊ธฐ์๋ ํค๊ฐ ์ธ๋ฑ์ค๊ฐ ๋๋ค.
// length ๋ ๋ฐฐ์ด์ด ๊ฐ์ง๊ณ ์๋ ์์ฑ์ด๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ์ถ๊ฐ๋์ง ์๋๋ค.
// length ๋ฅผ 3์ผ๋ก ์ง์ ํ๋ฉด ['a', 'b', 'undefined'] ๊ฐ ๋๋ค.
const arr6 = Array.from({
0: 'a',
1: 'b',
length: 2,
});
console.log(arr6); // [ 'a', 'b' ]์ ๊ทผ
์ถ๊ฐ
์ญ์
Array ์์ฑ, ๋ฉ์๋
Ref
Last updated