π§ λ°°μ΄
μ¬λ¬ κ°μ κ°μ μμ°¨μ μΌλ‘ λμ΄ν μλ£κ΅¬μ‘°.
λ°°μ΄ μμλ μμλ₯Ό κ°μ§κ³ μκ³ , κ° μμλ λ°°μ΄ μμμ μΈλ±μ€λ₯Ό κ°μ§κ³ μλ€.
μΈλ±μ€λ 0λΆν° μμνκ³ , κ° μμμ μΈλ±μ€λ₯Ό μ¬μ©νλ©΄ μμμ μ κ·Όν μ μλ€.
const arr = ['a', 'b', 'c'];
arr[0]; //a
arr[1]; //b
arr[2]; //c
π§ λ°°μ΄ μμ±
λ°°μ΄μ λ€μν λ°©λ²μΌλ‘ μμ±ν μ μλ€.
const array1 = new Array(1, 2);
console.log(array1); //[ 1, 2 ]
const array2 = Array.of(1, 2);
console.log(array2); //[ 1, 2 ]
const array3 = [1, 2];
console.log(array3); //[ 1, 2 ]
const array4 = Array.from(array2); //[ 1, 2 ]
console.log(array4);
π§ λ°°μ΄ μμ μ°Έμ‘°
λ°°μ΄ μμλ₯Ό μ°Έμ‘°ν λλ λκ΄νΈλ₯Ό μ¬μ©νλ€.
μμλ§λ€ μΈλ±μ€κ° μκΈ° λλ¬Έμ λκ΄νΈμ μΈλ±μ€λ₯Ό λ£μ΄ μ¬μ©νλ€.
λ§μ½ μ‘΄μ¬νμ§ μλ μμμ μ κ·Όνλ€λ©΄ undefinedκ° λ°νλλ€.
λν, λ°°μ΄ μμ μ λΆμ μ κ·Όνκ³ μΆλ€λ©΄ forλ¬Έμ μ¬μ©ν μ μλ€.
const food = ['π', 'π', 'π'];
console.log(food[1]); //π
console.log(food[5]); //undefined
for (let i = 0; i < food.length; i++) {
console.log(food[i]);
//π
//π
//π
}
π§ λ°°μ΄ μμμ λ³κ²½
λ°°μ΄μ μμλ₯Ό μΆκ°, κ°±μ , μμ ν μ μλ€.
λ§μ½ λ°°μ΄μ μμκ° μλ μΈλ±μ€λ₯Ό μ¬μ©νλ©΄ μμκ° κ°±μ λκ³ ,
μλ μΈλ±μ€λ₯Ό μ¬μ©νλ©΄ ν΄λΉ μΈλ±μ€μ μμκ° μΆκ°λλ€.
const food = ['π', 'π', 'π'];
food[1] = 'β¨'; //[ 'π', 'β¨', 'π' ]
food[5] = 'π€ͺ'; //[ 'π', 'β¨', 'π', <2 empty items>, 'π€ͺ' ]
μΈλ±μ€λ μμμ μμΉλ₯Ό λνλ΄λ―λ‘ μ μ νΉμ μ μ ννμ λ¬Έμμ΄μ μ¬μ©ν΄μΌ νλ€.
λ§μ½ μ΄μΈμ κ°μ μ¬μ©ν κ²½μ° νλ‘νΌν°κ° μμ±λλ€.
const food = ['π', 'π', 'π'];
food[1.234] = 'π';
console.log(food); //[ 'π', 'π', 'π', '1.234': 'π' ]
λ°°μ΄ μμ μ κ²½μ° deleteλ₯Ό μ¬μ©ν΄μ μμ ν μ μλ€.
μ΄λ μλ°μ€ν¬λ¦½νΈ λ°°μ΄μ΄ μΌλ°μ μΈ λ°°μ΄μ λμμ νλ΄ λΈ 'κ°μ²΄'μ΄κΈ° λλ¬Έμ κ°λ₯ν κ²μ΄λ€.
const food = ['π', 'π', 'π'];
delete food[0];
console.log(food); //[ <1 empty item>, 'π', 'π' ]
π§ λ°°μ΄ λ©μλ
μλ°μ€ν¬λ¦½νΈ λ°°μ΄μλ λ€μν λΉνΈμΈ λ©μλκ° μλ€.
λ°°μ΄ μ체λ₯Ό λ³κ²½νλ λ©μλκ° μκ³ , μλ‘μ΄ λ°°μ΄μ λ°ννλ λ©μλκ° μμ΄ μ£Όμνλ©° μ¬μ©ν΄μΌ νλ€.
Array.isArray
νΉμ ν μ€λΈμ νΈκ° λ°°μ΄μΈμ§ νμΈνλ λ©μλμ΄λ€.
λ°°μ΄μ΄ λ§λ€λ©΄ true, μλλΌλ©΄ falseλ₯Ό λ°ννλ€.
const food = ['π', 'π', 'π'];
console.log(Array.isArray(food)); //true
console.log(Array.isArray({})); //false
Array.prototype.indexOf
νΉμ ν μμμ μμΉλ₯Ό μ°Ύμ λ μ¬μ©νλ λ©μλμ΄λ€.
λ§μ½ νΉμ ν μμκ° μ¬λ¬κ°λΌλ©΄ 첫 λ²μ§Έλ‘ κ²μλ μμμ μΈλ±μ€λ₯Ό λ°ννκ³ ,
μμκ° μ‘΄μ¬νμ§ μλλ€λ©΄ -1μ λ°ννλ€.
const food = ['π', 'π', 'π'];
console.log(food.indexOf('π')); //-1
console.log(food.indexOf('π')); //2
Array.prototype.includes
λ°°μ΄ μμ νΉμ ν μμκ° μλμ§ νμΈνλ λ©μλμ΄λ€.
μλ€λ©΄ true, μλ€λ©΄ falseλ₯Ό λ°ννλ€.
const food = ['π', 'π', 'π'];
console.log(food.includes('π')); //true
Array.prototype.unshift
λ°°μ΄μ μ μΌ μ²μμΌλ‘ μμλ₯Ό μΆκ°νλ λ©μλμ΄λ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const food = ['π', 'π', 'π'];
food.unshift('π₯');
console.log(food); //['π₯', 'π', 'π', 'π' ]
Array.prototype.push
λ°°μ΄μ μ μΌ λ§μ§λ§μΌλ‘ μμλ₯Ό μΆκ°νλ λ©μλμ΄λ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const food = ['π', 'π', 'π'];
food.push('π₯');
console.log(food); //['π', 'π', 'π', 'π₯' ]
Array.prototype.shift
λ°°μ΄μ 첫λ²μ§Έ μμλ₯Ό μ κ±°νκ³ κ·Έ μμλ₯Ό λ°ννλ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const food = ['π', 'π', 'π'];
const firstFood = food.shift();
console.log(firstFood); //π
console.log(food); // ['π', 'π'];
Array.prototype.pop
λ°°μ΄μ λ§μ§λ§ μμλ₯Ό μ κ±°νκ³ κ·Έ μμλ₯Ό λ°ννλ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const food = ['π', 'π', 'π'];
const lastFood = food.pop();
console.log(lastFood); //π
console.log(food); //['π', 'π' ];
Array.prototype.concat
μ¬λ¬ κ°μ λ°°μ΄μ λΆμ¬μ£Όλ λ©μλμ΄λ€.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = arr1.concat(arr2);
console.log(arr1);
console.log(arr2);
console.log(arr3); //[ 1, 2, 3, 4, 5, 6 ]
Array.prototype.splice
μλ³Έ λ°°μ΄μ μ€κ°μ μμλ₯Ό μΆκ°νκ±°λ μ κ±°ν λ μ¬μ©νλ λ©μλμ΄λ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const food = ['π', 'π', 'π'];
const pick = food.splice(1, 1);
console.log(food); //['π', 'π']
console.log(pick); //[ 'π' ]
//μΈλ±μ€ 1λΆν° 1κ°μ μμ μ κ±°νκ³ , κ·Έ μ리μ π, π₯€ μ½μ
food.splice(1, 1, 'π', 'π₯€');
console.log(food); //[ 'π', 'π', 'π₯€' ]
Array.prototype.slice
μλ³Έ λ°°μ΄μ μλΌ μλ‘μ΄ λ°°μ΄μ λ§λλ λ©μλμ΄λ€.
const food = ['π', 'π', 'π'];
const pick = food.slice(0, 2);
console.log(pick); //[ 'π', 'π' ]
console.log(food); //['π', 'π', 'π']; //μλ³Έ λ°°μ΄μ λ³νκ° μλ€
Array.prototype.reverse
μλ³Έ λ°°μ΄μ μμλ₯Ό λ°λλ‘ λ€μ§λ λ©μλμ΄λ€.
const arr = [1, 2, 3, 4, 5, 6]
const reverseArr = arr.reverse();
console.log(reverseArr); //[ 6, 5, 4, 3, 2, 1 ]
Array.prototype.join
μλ³Έ λ°°μ΄μ λͺ¨λ μμλ₯Ό λ¬Έμμ΄λ‘ λ³ννκ³ , μΈμλ‘ λ°μ ꡬλΆμλ‘ λ¬Έμμ΄μ μ°κ²°νμ¬ λ°ννλ λ©μλμ΄λ€.
const arr = [1, 2, 3, 4, 5];
const joinArr = arr.join(' ');
console.log(joinArr); //1 2 3 4 5
const joinArr2 = arr.join('');
console.log(joinArr2); //12345
Array.prototype.flat
μ€μ²© λ°°μ΄μ νλμ λ°°μ΄λ‘ νννκ² λ§λλ λ©μλμ΄λ€.
κΉμ΄μ κΈ°λ³Έκ°μ 1μ΄λ€.
let arr = [
[1, [2, 3]],
[4, [5, 6]],
];
console.log(arr.flat()); //[ 1, [ 2, 3 ], 4, [ 5, 6 ] ]
console.log(arr.flat(2)); //[ 1, 2, 3, 4, 5, 6 ]
Array.prototype.fill
μΈμλ‘ λ°μ νΉμ ν κ°μΌλ‘ μλ³Έ λ°°μ΄μ λ€ μ±μ°λ λ©μλμ΄λ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const arr = [1, 2, 3, 4, 5, 6];
arr.fill(0);
console.log(arr); //[ 0, 0, 0, 0, 0, 0 ]
//μΈμλ‘ μ λ¬λ°μ '!'λ₯Ό μΈλ±μ€ 1λΆν° 3 μ΄μ κΉμ§ μ±μ΄λ€.
arr.fill('!', 1, 3);
console.log(arr); //[ 0, '!', '!', 0, 0, 0 ]
π§ λ°°μ΄ κ³ μ°¨ ν¨μ
κ³ μ°¨ ν¨μλ ν¨μλ₯Ό μΈμλ‘ μ λ¬λ°κ±°λ ν¨μλ₯Ό λ°ννλ ν¨μλ₯Ό λ§νλ€.
Array.prototype.sort
λ°°μ΄μ μμλ₯Ό μ λ ¬νλ€.
κΈ°λ³Έμ μΌλ‘ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ€.
μλ³Έ λ°°μ΄μ μ§μ λ³κ²½νλ€.
const texts = ['javaScript', 'css'];
texts.sort();
console.log(texts); //[ 'css', 'javaScript' ]
λ€λ§, μ«μ μ λ ¬μ κ²½μ° μ£Όμκ° νμνλ€.
λ°°μ΄μ μμλ₯Ό μΌμμ μΌλ‘ λ¬Έμμ΄λ‘ λ³νν΄ μ λ ¬νκΈ° λλ¬Έμ΄λ€.
const numbers = [0, 1, 5, 3, 10];
numbers.sort();
console.log(numbers); //[ 0, 1, 10, 3, 5 ]
λ°λΌμ μ«μλ₯Ό μ λ ¬ν λλ λΉκ΅ ν¨μλ₯Ό μΈμλ‘ μ λ¬ν΄μΌ νλ€.
λΉκ΅ ν¨μμ λ°νκ°μ΄ 0λ³΄λ€ μμΌλ©΄ μ€λ¦μ°¨μ, 0λ³΄λ€ ν¬λ©΄ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬νλ€.
const numbers = [0, 1, 5, 3, 10];
numbers.sort((a, b) => a - b);
console.log(numbers); //[ 0, 1, 3, 5, 10 ]
Array.prototype.forEach
forλ¬Έ λμ μ¬μ©ν μ μλ€.
λ΄λΆμμ λ°λ³΅λ¬Έμ ν΅ν΄ λ°°μ΄μ μννλ©΄μ μ½λ°± ν¨μλ₯Ό λ°λ³΅ νΈμΆνλ€.
const numbers = [1, 2, 3];
const answer = [];
numbers.forEach((numbers) => answer.push(numbers * 2));
console.log(answer); //[ 2, 4, 6 ]
Array.prototype.find
λ°°μ΄μ μννλ©΄μ 쑰건μ λ§λ 첫 λ²μ§Έ μμλ₯Ό λ°ννλ€.
const food = [
{ name: 'pizza', emoji: 'π' },
{ name: 'sushi', emoji: 'π£' },
{ name: 'ramen', emoji: 'π' },
];
const answer = food.find((value) => value.name === 'pizza');
console.log(answer); //{ name: 'pizza', emoji: 'π' }
Array.prototype.findIndex
λ°°μ΄μ μννλ©΄μ 쑰건μ λ§λ 첫λ²μ§Έ μμμ μΈλ±μ€λ₯Ό λ°ννλ€.
const food = [
{ name: 'pizza', emoji: 'π' },
{ name: 'sushi', emoji: 'π£' },
{ name: 'ramen', emoji: 'π' },
{ name: 'sushi', emoji: 'π' },
];
const answer = food.findIndex((value) => value.name === 'sushi');
console.log(answer); //1
Array.prototype.map
forλ¬Έ λμ μ¬μ©ν μ μλ€.
λ΄λΆμμ λ°λ³΅λ¬Έμ ν΅ν΄ λ°°μ΄μ μννλ©΄μ μ½λ°± ν¨μλ₯Ό λ°λ³΅ νΈμΆνλ€.
μ½λ°± ν¨μμ λ°νκ°λ€λ‘ ꡬμ±λ μλ‘μ΄ λ°°μ΄μ λ°ννλ€.
const numbers = [1, 2, 3];
const answer = numbers.map((number) => number * 2);
console.log(answer); //[ 2, 4, 6 ]
Array.prototype.flatMap
mapμ ν΅ν΄ μμ±λ λ°°μ΄μ ννννλ€.
const numbers = [1, 2, 3];
let answer = numbers.map((number) => [1, 2, 3]);
//[ [ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ] ]
answer = numbers.flatMap((number) => [1, 2, 3]);
console.log(answer); /**
[
1, 2, 3, 1, 2,
3, 1, 2, 3
] */
Array.prototype.some
λ°°μ΄μ μννλ©΄μ λΆλΆμ μΌλ‘ 쑰건μ λ§λμ§ νμΈνλ€.
λ¨ νλ²μ΄λΌλ μ°Έμ΄λ©΄ true, κ·Έλ μ§ μλ€λ©΄ falseλ₯Ό λ°ννλ€.
const food = [
{ name: 'pizza', emoji: 'π' },
{ name: 'sushi', emoji: 'π£' },
{ name: 'ramen', emoji: 'π' },
];
const result = food.some((item) => item.name === 'pizza');
console.log(result); //true
Array.prototype.every
λ°°μ΄μ μννλ©΄μ μ 체μ μΌλ‘ 쑰건μ λ§λμ§ νμΈνλ€.
λͺ¨λ μ°Έμ΄λ©΄ true, κ·Έλ μ§ μλ€λ©΄ falseλ₯Ό λ°ννλ€.
const food = [
{ name: 'pizza', emoji: 'π' },
{ name: 'sushi', emoji: 'π£' },
{ name: 'ramen', emoji: 'π' },
];
const result = food.every((item) => item.name === 'pizza');
console.log(result); //false
Array.prototype.filter
λ°°μ΄μ μννλ©΄μ μ½λ°±ν¨μλ₯Ό λ°λ³΅ νΈμΆνλ€.
μ½λ°± ν¨μμ λ°νκ°μ΄ trueμΈ μμλ‘λ§ κ΅¬μ±λ μλ‘μ΄ λ°°μ΄μ λ°ννλ€.
μλ³Έ λ°°μ΄μ λ³κ²½λμ§ μλλ€.
const food = [
{ name: 'pizza', emoji: 'π' },
{ name: 'sushi', emoji: 'π£' },
{ name: 'ramen', emoji: 'π' },
{ name: 'sushi', emoji: 'π' },
];
const result = food.filter((item) => item.name === 'sushi');
console.log(result); //[ { name: 'sushi', emoji: 'π£' }, { name: 'sushi', emoji: 'π' } ]
Array.prototype.reduce
λ°°μ΄μ μννλ©΄μ μ½λ°± ν¨μλ₯Ό λ°λ³΅ νΈμΆνλ€.
μ½λ°± ν¨μμ λ°νκ°μ λ€μ μνμ μ½λ°± ν¨μμ 첫 λ²μ§Έ μΈμλ‘ μ λ¬νλ€.
첫 λ²μ§Έ μΈμλ‘ μ½λ°± ν¨μ, λ λ²μ§Έ μΈμλ‘ μ΄κΈ°κ°μ μ λ¬λ°λλ€.
const result = [1, 2, 3, 4, 5].reduce((sum, value) => (sum += value), 0);
console.log(result); //15
'JavaScript' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
setTimeoutκ³Ό setInterval (0) | 2023.11.02 |
---|---|
ν΄λμ€ (0) | 2023.02.24 |
κ°μ²΄ (0) | 2023.02.21 |
ν¨μ (0) | 2023.02.19 |
μ μ΄λ¬Έ (0) | 2023.02.19 |
λκΈ