'use strict'
// Array
// 1. Declaration
const arr1 = new Array();
const arr2 = [1, 2];
// 2. Index position
const fruits = ['π', 'π', 'π'];
const malza = new Array('(βΒ΄β‘`β)', '(β\'β‘\'β)', 'β°(*Β°β½Β°*)β―');
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
console.clear();
// 3. Looping over an array
for(let i = 0.; i < fruits.length; i++){
console.log(fruits[i]);
}
for(let fruit of fruits) {
console.log(fruit);
}
console.clear();
// fruits.forEach(function (fruit, index, array) {
// console.log(fruit, index, array); //λ³΄ν΅ arrayκΉμ§λ λ°μμ€μ§ μλλ€.
// });
π 0 (3)Β ["π", "π", "π"]
π 1 (3)Β ["π", "π", "π"]
π 2 (3)Β ["π", "π", "π"] //μμ μμ μ΄λ κ² μΆλ ₯λλ€.
// fruits.forEach((fruit, index, array) => {
// console.log(fruit, index, array);
// });
fruits.forEach((fruit) => console.log(fruit));
// 4. Addition, deletion, copy
// push: add an item to the end
fruits.push('π₯¨');
console.log(fruits);
// pop: remove an item from the end
fruits.pop();
console.log(fruits);
//unshift: add an item to the beginning
fruits.unshift('π'); // 맨 μμμ λ£λλ€.
console.log(fruits);
//shift: remove an item from the beginning
fruits.shift(); // 맨 μμμ λΊλ€.
console.log(fruits);
// note!! shift, unshift are slower than pop, push
// μμμ λΉΌκ±°λ λ£μΌλ©΄ κ·Έλ§νΌ λ€μμ λ‘κΈ°κ±°λ λ°μ΄μΌ νλκΉ
// splice: remove an item by index position
fruits.push('π', 'π');
console.log(fruits); // (5)Β ["π", "π", "π", "π", "π"]
fruits.splice(1, 2);
console.log(fruits); // 1λ²λΆν° 2κ° μ§μ°λκΉ (3)Β ["π", "π", "π"]
//fruits.splice(1); // λͺκ° μ§μΈμ§ μ λ겨주면 1λ²λΆν° λ€ μ§μλ²λ¦°λ€.
fruits.splice(1, 1, 'π', 'π'); // 1λ² λΆν° 1κ° μ§μ°κ³ κ·Έ μ리μ λ€μκ²λ€ λ£μ΄μ€
console.log(fruits); // (4)Β ["π", "π", "π", "π"]
//splice λ μμ λ elem μ return ν΄μ€λ€.
// combine two arrays
const fruits2 = ['π₯', 'π₯'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits); // ν©μ³μ§ κ²μ΄ μκ²Όλ€. // (6)Β ["π", "π", "π", "π", "π₯", "π₯"]
// 5. Searching
// find the index
console.clear();
console.log(fruits);
console.log(fruits.indexOf('π')); // 1 return ν΄μ€λ€.
console.log(fruits.includes('π³')); // μ‘΄μ¬ νλμ§ μ¬λΆ. false return ν΄μ€λ€.
console.log(fruits.includes('π'));
console.log(fruits.indexOf('π³')); // μμΌλκΉ -1 retrun ν΄μ€λ€.
// lastIndexof
console.clear();
newFruits.push('π');
console.log(newFruits.lastIndexOf('π')); //λ§μ§λ§μ λ€μ΄κ°μΌλκΉ λ§μ§λ§ idxλ₯Ό return λ°μ κ²μ΄λ€.
console.log(malza);