'use strict';
//Array
//1. Declaration
const arr1 = new Array();
const arr2 = [1,2];
//2. Index position
const fruits = ['๐', '๐'];
console.log(fruits);
console.log(fruits.length);
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[fruits.length-1]);//๋ง์ง๋ง ๋ฐ์ดํฐ ์ฐพ๊ธฐ
//3. Looping over an array ๋ฌดํ๋ฃจํ ๋๋ฆฌ๊ธฐ
//print all fruits
console.clear();
//a. for
for(let i=0; i<fruits.length; i++){
console.log(fruits[i]);
}
// b. for of
for(let fruit of fruits){
console.log(fruit);
}
//c. forEach & ์ด๋ฆ์ด ์๋ ํจ์๋ ํ์ดํ ํจ์ ์ฌ์ฉํ๋ค. (ํ์ค๋ง ์๋ ๊ฒฝ์ฐ๋ {} ์ญ์ ๊ฐ๋ฅํจ)
fruits.forEach((fruit)=> console.log(fruit));
//๋ฐฐ์ด์ Addtion, delete, 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);//์์ดํ
ํ๊ฐ ๋น ์ง
// ์์์๋ถํฐ ์์ดํ
๋ฃ๊ธฐ : add an item to the beginning (unshift)
fruits.unshift('๐
','๐ฅ');
console.log(fruits);
// ์์์๋ถํฐ ์์ดํ
๋นผ๊ธฐ : remove an item from the beginning (shitf)
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);
fruits.splice(1,2);
console.log(fruits);
fruits.splice(1,1,'๐','โช๏ธ');//์ญ์ ๋ฐ ์ถ๊ฐ๋ ๊ฐ๋ฅ
console.log(fruits);
//concat : combine tow arrays
const fruits2 = ['โ๏ธ','โค'];
const newFruits = fruits.concat(fruits2);
console.log(newFruits);
//5. Searching (indexOf, includes)
//find the index --> ๋ฐฐ์ด์์ ํด๋น๊ฐ์ด ์์๊ฒฝ์ฐ ? -1 ์ถ๋ ฅํ๋ค.
console.clear();
console.log(fruits);
console.log(fruits.indexOf('๐ฅ'));
console.log(fruits.includes('๐
'));//f or t ๋ก ๋ฐํ
//lastIndexOf
console.clear();
fruits.push('๐
');
console.log(fruits.indexOf('๐
'));
console.log(fruits.lastIndexOf('๐
'));