μλ°μ€ν¬λ¦½νΈ λ°°μ΄ Array
π μλ°μ€ν¬λ¦½νΈμ λ°°μ΄μ λ°°μ΄μ΄ μλλ€?
μλ£ κ΅¬μ‘°data structureμμ λ§νλ "λ°°μ΄" === λμΌν ν¬κΈ°μ λ©λͺ¨λ¦¬ 곡κ°μ΄ λΉνμμ΄ μ°μμ μΌλ‘ λμ΄λ μλ£ κ΅¬μ‘°.
μ¦, "λ°°μ΄"μ μμλ νλμ λ°μ΄ν° νμ μΌλ‘ ν΅μΌλμ΄ μμΌλ©°, μλ‘ μ°μμ μΌλ‘ μΈμ ν΄ μλ€. ( λ°μ§ λ°°μ΄dense array )
νλμ λ°μ΄ν° νμ μΌλ‘ ν΅μΌλμ΄μλ€λ μκΈ°λ κ° μμκ° λμΌν λ°μ΄ν° ν¬κΈ°λ₯Ό κ°μ§λ€λ λ»κ³Ό μΌλ§₯μν΅νλ€.
μΌλ°μ μΈ λ°°μ΄ μ₯λ¨μ
μ₯μ : μΈλ±μ€λ‘ μμμ λΉ λ₯΄κ² μ κ·Όν μ μλ€.
λ¨μ : νΉμ μμλ₯Ό κ²μ/μ½μ /μμ νλ κ²½μ°μλ λΉν¨μ¨μ μ΄λ€.
μ¦, μλ°μ€ν¬λ¦½νΈμ λ°°μ΄μ μ°μμ μΌλ‘ μ΄μ΄μ Έ μμ§ μμ μ μλ€. ex. ν¬μ λ°°μ΄
μ¦, μλ°μ€ν¬λ¦½νΈμ λ°°μ΄μ μμλ₯Ό μν κ°κ°μ λ©λͺ¨λ¦¬ 곡κ°μ΄ λμΌν ν¬κΈ°λ₯Ό κ°μ§ μμλ λλ€. ex. μμλ₯Ό μ’ λ₯λ³λ‘ λ΄μ μ μλ€
μλ°μ€ν¬λ¦½νΈμ λ°°μ΄ μ₯λ¨μ
μ₯μ : νΉμ μμλ₯Ό κ²μ/μ½μ /μμ νλ κ²½μ°μ μΌλ°μ μΈ λ°°μ΄λ³΄λ€ λΉ λ₯Ό μ μλ€.
λ¨μ : μΈλ±μ€λ‘ μμμ μ κ·Όν λ μΌλ° λ°°μ΄λ³΄λ€ λ리λ€.
--> μλ°μ€ν¬λ¦½νΈμ λ°°μ΄μ μΌλ°μ μΈ λ°°μ΄μ λμμ νλ΄ λΈ νΉμν κ°μ²΄λ€.
π λ°°μ΄ λ¦¬ν°λ΄
λ°°μ΄μ κ°μ²΄μ§λ§ νλ‘νΌν° ν€κ° μκ³ κ°λ§ μ‘΄μ¬νλ€.
// λ°°μ΄ λ¦¬ν°λ΄μ 0κ° μ΄μμ μμλ₯Ό, μΌνλ‘ κ΅¬λΆνμ¬ λκ΄νΈλ‘ λ¬Άλλ€.
const arr = [1, 2, 3];
console.log(arr.length); // 3
// λ°°μ΄ λ¦¬ν°λ΄μ μμλ₯Ό νλλ μΆκ°νμ§ μμΌλ©΄, λ°°μ΄μ κΈΈμ΄λ 0μΈ λΉ λ°°μ΄μ΄ λλ€.
const arr = [];
console.log(arr.length); // 0
// λ°°μ΄ λ¦¬ν°λ΄μ μμλ₯Ό μλ΅νλ©΄, "ν¬μ λ°°μ΄"μ΄ μμ±λλ€.
const arr = [1, , 3];
console.log(arr.length) // 3
// ν¬μ λ°°μ΄
// ν¬μ λ°°μ΄μ λ°°μ΄ κΈΈμ΄λ κ°μλ° μμ κ° κ°μλ μ μ ;;
// OMG μ΅μ
π λ°°μ΄ μ’ λ₯?
βΎ Array μμ±μ ν¨μ
const arr = new Array(10);
console.log(arr); // [empty x 10]
console.log(arr.length); // 10
// μ΄λ μμ±λ λ°°μ΄ -> ν¬μ λ°°μ΄
// length νλ‘νΌν° κ°μ 0μ΄ μλμ§λ§, μ€μ λ‘ λ°°μ΄μ μμλ μ‘΄μ¬νμ§ μλλ€.
console.log(Object.getOwnPropertyDescriptors(arr));
// length: configurable: false, enumerable: false, value: 10, writable: true
// λ°°μ΄μ μμλ₯Ό μ΅λ (2^32 - 1 = 4,294,967,295)κ° κ°μ§ μ μλ€.
new Array(4294967295);
// μ λ¬λ μΈμκ° 0~4,294,967,295λ₯Ό λ²μ΄λλ©΄ RangeErrorκ° λ°μνλ€.
// μ λ¬λ μΈμκ° μμμ¬λ RangeErrorκ° λ°μνλ€.
new Array(4294967296); // RangeError: Invalid array length
new Array(-1); // RangeError: Invalid array length
// μ λ¬λ μΈμκ° μλ κ²½μ°, λΉ λ°°μ΄μ μμ±νλ€ !
new Array(); // -> []
// μ λ¬λ μΈμ κ°μ >= 2 -> λ°°μ΄ μμ±
new Array(1, 2, 3); // -> [1, 2, 3]
// μ λ¬λ μΈμ κ°μ <= 1 && !typeof μΈμ === 'number' -> λ°°μ΄ μμ±
new Array({}); // -> [{}]
// new μ°μ°μμ νΈμΆνμ§μλλΌλ, μΌλ° ν¨μλ‘μ νΈμΆν΄λ λμνλ€.
Array(1, 2, 3); // -> [1, 2, 3]
βΎ Array.of (ES6 λμ )
Array μμ±μ ν¨μμ λ€λ₯΄κ², μ λ¬λ μΈμκ° 1κ°μ¬λ κ·Έ κ°μ νμ μ μκ΄μμ΄ λ°°μ΄μ μμ±νλ€ !
Array.of(1); // -> [1]
Array.of(1, 2, 3); // -> [1, 2, 3]
Array.of('string'); // -> ['string']
βΎ Array.from (ES6 λμ )
μ μ¬ λ°°μ΄ κ°μ²΄array-like objectλλ μ΄ν°λ¬λΈ κ°μ²΄iterable objectλ₯Ό μΈμλ‘ μ λ¬λ°μ λ°°μ΄λ‘ λ³ννμ¬ λ°ννλ€.
Array.from({length: 2, 0: 'a', 1: 'b'}); // -> ['a', 'b']
Array.from('Hello'); // -> ['H','e','l','l','o']
π μ μ¬ λ°°μ΄ κ°μ²΄ array-like object?
01 λ§μΉ λ°°μ΄μ²λΌ μΈλ±μ€λ‘ νλ‘νΌν° κ°μ μ κ·Όν μ μλ€.
02 length νλ‘νΌν°λ₯Ό κ°λ κ°μ²΄μ΄λ€.
// μ μ¬ λ°°μ΄ κ°μ²΄
const arrayLike = {
'0': 'apple',
'1': 'banana',
'2': 'orange',
length: 3
}
// μ μ¬ λ°°μ΄ κ°μ²΄λ λ§μΉ λ°°μ΄μ²λΌ forλ¬ΈμΌλ‘ μνν μ μλ€.
for(let i=0; i<arrayLike.length; i++){
console.log(arrayLike[i]); // apple banana orange
}
π μ΄ν°λ¬λΈ iterable object?
01 Symbol.iterator λ©μλλ₯Ό ꡬννμ¬ for...of λ¬ΈμΌλ‘ μνν μ μλ€.
02 μ€νλ λ λ¬Έλ², λ°°μ΄ λμ€νΈλμ²λ§ ν λΉμ λμμΌλ‘ μ¬μ©ν μ μλ κ°μ²΄μ΄λ€.
--> μ§κΈμΌλ‘μλ μ΄ν΄κ° νλλ μκ°μ§λ§ μΌλ¨ PASS ^_^
03 ES6μμ μ 곡νλ built-in μ΄ν°λ¬λΈμ Array, String, Map, Set, DOM Collection(NodeList, HTMLCollection), arguments..
π λ°°μ΄ μμμ μ°Έμ‘°
π μ°Έμ‘°
const arr = [1, 2];
// μΈλ±μ€κ° 0μΈ μμλ₯Ό μ°Έμ‘°
console.log(arr[0]); // 1
μΈλ±μ€λ κ°μ μ°Έμ‘°ν μ μλ€λ μλ―Έμμ κ°μ²΄μ νλ‘νΌν° ν€μ κ°μ μν μ νλ€.
const arr = [1, 2];
// λ°°μ΄ arrμλ μΈλ±μ€κ° 2μΈ μμκ° μ‘΄μ¬νμ§ μλλ€.
console.log(arr[2]); // undefined
μ‘΄μ¬νμ§ μλ μμμ μ κ·Όνλ©΄ undefinedκ° λ°νλλ€.
λ°°μ΄μ μ¬μ€, μΈλ±μ€λ₯Ό λνλ΄λ λ¬Έμμ΄μ νλ‘νΌν° ν€λ‘ κ°λ κ°μ²΄λ€.
μ‘΄μ¬νμ§ μλ νλ‘νΌν° ν€λ‘ κ°μ²΄μ νλ‘νΌν°μ μ κ·Όνμ λ undefinedμ λ°ννλ κ²μ²λΌ
λ°°μ΄λ μ‘΄μ¬νμ§ μλ μμλ₯Ό μ°Έμ‘°νλ©΄ undefinedλ₯Ό λ°ννλ€.
// ν¬μ λ°°μ΄
const arr = [1, , 3];
console.log(arr[1]); // undefined
console.log(arr[3]); // undefined
λ§μ°¬κ°μ§λ‘ ν¬μ λ°°μ΄μ μ‘΄μ¬νμ§ μλ μμλ₯Ό μ°Έμ‘°ν΄λ undefinedκ° λ°νλλ€.
π λ°°μ΄ μμμ μΆκ° & κ°±μ
π μΆκ°
const arr = [0];
// λ°°μ΄ μμμ μΆκ°
arr[1] = 1;
console.log(arr); // [0, 1]
console.log(arr.length); // 2
κ°μ²΄μ νλ‘νΌν° ν€λ₯Ό λμ μΌλ‘ μΆκ°ν μ μλ κ²μ²λΌ λ°°μ΄μλ κ·Έ μμλ₯Ό λμ μΌλ‘ μΆκ°ν μ μλ€.
μ΄λ length νλ‘νΌν° κ°μ μλκ°±μ λλ€.
arr[100] = 100;
console.log(arr); // [0, 1, empty*98, 100]
console.log(arr.length); // 101
νμ¬ λ°°μ΄μ length νλ‘νΌν° κ°λ³΄λ€ ν° μΈλ±μ€λ‘ μλ‘μ΄ μμλ₯Ό μΆκ°νλ©΄ ν¬μ λ°°μ΄μ΄ λλ€.
λͺ μμ μΌλ‘ μΈλ±μ€λ‘ μμμ μ κ·Όνμ¬ κ°μ ν λΉνμ§ μμ μμλ μμ±λμ§ μλλ€λ κ²μ μ£Όμνμ.
π κ°±μ
// μμκ°μ κ°±μ
arr[1] = 10;
console.log(arr); // [0, 10, empty*98, 100]
μ΄λ―Έ μμκ° μ‘΄μ¬νλ μΈλ±μ€μ κ°μ μ¬ν λΉνλ©΄ μμκ°μ΄ κ°±μ λλ€.
const arr = [];
// λ°°μ΄ μμμ μΆκ°
arr[0] = 1;
arr['1'] = 2;
// νλ‘νΌν° μΆκ°
arr['foo'] = 3;
arr.var = 4;
arr[1.1] = 5;
arr[-1] = 6;
console.log(arr); // [1, 2, foo:3, bar:4, '1.1':5, '-1':6]
// νλ‘νΌν°λ lengthμ μν₯μ μ£Όμ§ μλλ€.
console.log(arr.length); // 2
μΈλ±μ€λ μμμ μμΉλ₯Ό λνλ΄λ―λ‘ λ°λμ 0μ΄μμ μ μ( λλ μ μ ννμ λ¬Έμμ΄ )λ₯Ό μ¬μ©ν΄μΌ νλ€.
μ μ μ΄μΈμ κ°μ μΈλ±μ€μ²λΌ μ¬μ©νλ©΄, μμκ° μμ±λλ κ²μ΄ μλλΌ νλ‘νΌν°κ° μμ±λλ€.
μ΄λ μΆκ°λ νλ‘νΌν°λ length νλ‘νΌν° κ°μ μν₯μ μ£Όμ§ μλλ€.
π λ°°μ΄ μμμ μμ
π μμ
const arr = [1, 2, 3];
// λ°°μ΄ μμμ μμ
delete arr[1];
console.log(arr); // [1, empty, 3]
// length νλ‘νΌν°μ μν₯μ μ£Όμ§ μλλ€. -> ν¬μ λ°°μ΄μ΄ λλ€.
console.log(arr.length); // 3
delete μ°μ°μλ κ°μ²΄μ νλ‘νΌν°λ₯Ό μμ νμ§λ§ ν¬μ λ°°μ΄μ μμ±νλ€.
μ΄ μ°μ°μλ μ¬μ©νμ§ μλ κ²μ΄ μ’λ€.
ν¬μ λ°°μ΄μ λ§λ€μ§ μμΌλ©΄μ λ°°μ΄μ νΉμ μμλ₯Ό μμ ν μμ νλ €λ©΄ Array.prototype.splice λ©μλλ₯Ό μ¬μ©νλ©΄λλΉ :)