ํน์ ํ ๋ฐ์ดํฐ๋ฅผ ์ผ๋ ฌ๋ก ์ผ์ ํ๊ฒ ๋ชจ์๋์ ์งํฉ์ด๋ค.
let myArray1 = [19, 44, 'good', false]
index position์ ์ฌ์ฉํ์ฌ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
let myArray2 = [3, 4, 5, 6]
//์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
let first = myArray2[1]
console.log(first) //4
//๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
console.log(myArray2[3]) //6
myArray2[0] = 7
console.log(myArray2)
//[7, 4, 5, 6]
console.log(myArray2.length)
//4
myArray2.push(8)
console.log(myArray2)
//[7, 4, 5, 6, 8]
//๋ฐฐ์ด์ ๋ง์ง๋ง ์์ ์ญ์
myArray2.pop()
console.log(myArray2)
//[7, 4, 5, 6]
//๋ฐฐ์ด์ ์ฒซ๋ฒ์งธ ์์ ์ญ์ธ
myArray2.shift()
console.log(myArray2)
//[4, 5, 6]
//์ฌ๋ฌ๊ฐ์ง ๊ธฐ๋ฅ์ด ์์ผ๋ ๊ฒ์์ ํด์ ์ํฉ์ ๋ง๋ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์.
let myArray3 = [1, 2, [1, 2, 3], 3]
console.log(myArray3[2])
//[1, 2, 3]
console.log(myArray3[2][1])
//2
let myArray4 = [1, 2, 3, 4]
let myArray5 = [5, 6]
let myArray6 = [7, 8, 9]
console.log(myArray4[3]) //4
console.log(myArray5[1]) //6
console.log(myArray6[2]) //3
๋ฐฐ์ด์ ์์๊ฐ ๋ง์ ์ํ์์ ๋ง์ง๋ง ์์๊ฐ ๋ช ๋ฒ์งธ์ ์๋์ง ์๊ธฐ ์ด๋ ต๋ค. ๋ฐ๋ผ์ ๋ฐฐ์ด์ length๋ฅผ ๊ตฌํ ๋ค, ๋ง์ง๋ง index์๋ length-1๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค. ์ด๋ index๊ฐ 0๋ถํฐ ์์ํ๊ธฐ ๋๋ฌธ์ด๋ค.
console.log(myArray[myArray4.length - 1])
//console.log(myArray[3])
//4
index๋ฒ์์ ๋ฐ๋ผ ์๋ก์ด ๋ฐฐ์ด์ ๋ฆฌํดํ๋ฉฐ, ์๋ณธ ๋ฐฐ์ด์ ๋ณํ์ํค์ง ์๋๋ค. ๋ฐ๋ผ์ ๊ผญ ์๋ก์ด ๋ณ์๋ฉด์ ํ ๋นํด์ฃผ๊ณ ์ฌ์ฉํด์ผํ๋ค.
//slice(start, end)
let nums = [1, 2, 3, 4, 5, 6, 7]
let nums_new1 = nums.slice(1, 3)
console.log(nums_new1)
//[2, 3]
let nums_new2 = nums.slice(-2)
console.log(nums_new2)
//[6, 7]
//์์ ๋ค์ด๊ฐ ๊ฒฝ์ฐ ๋์์๋ถํฐ ์ซ์ ๋งํผ์ ์์๋ฅผ ๋ฐฐ์ด์ ๋ด์ ๋ฆฌํด
๋ฐฐ์ด ๋ด ํน์ ์์๋ฅผ ์ญ์ ํ๊ฑฐ๋, ๋ค๋ฅธ ์์๋ก ๋์นํ๊ฑฐ๋, ์๋ก์ด ์์๋ฅผ ์ถ๊ฐํ ๋ ์ฌ์ฉํ๋ค. ๋ณดํต ๋๊ธ ์ญ์ ๊ธฐ๋ฅ ๊ตฌํํ ๋ ๋ง์ด ์ฐ์ธ๋ค.
//splice(start, delete ์์ ๊ฐ์, item ์ถ๊ฐ์์, ...)
//2๋ฅผ 10์ผ๋ก ๋ฐ๊พธ์ด๋ณด์.
let num = [1, 2, 3, 4, 5, 6];
num.splice(1, 1, 10);
console.log(num);
//[1, 10, 3, 4, 5, 6]
//basket์์ '๊ณฐํก์ด' ์ญ์
let basket = [['์ํ','๊ณฐํก์ด'],['๊ณฐํก์ด','๋นต','๋ธ๊ธฐ์ผ'],['๊ทค','๊ณฐํก์ด','์ฌ๊ณผ']];
function removeGerm(arr) {
for(i=0; i < arr.length; i++) {
for(j=0; j < arr[i].length; j++) {
if(arr[i][j] === '๊ณฐํก์ด') {
arr[i].splice(j, 1);
}
}
}
return arr;
}
console.log(removeGerm(basket)) //[["์ํ"], ["๋นต", "๋ธ๊ธฐ์ผ"], ["๊ทค", "์ฌ๊ณผ"]]