๐ ์ด ํฌ์คํ ์์๋ JavaScript์ ๊ฐ์ฒด๋ฅผ ์ฐ๋ ์ด์ ์ ์ฌ์ฉ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
๐ฅ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ์ด์
๐ฅ "Bracket notation" & "Dot notation"
๐ฅ ๊ฐ์ฒด ๋ค๋ฃจ๊ธฐ(์ถ๊ฐ, ์์ , ์ญ์ )
โ๏ธ ์ฌ๋ฌ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ๋ณ์์ ํ ๋นํ ๋ ๋ฐฐ์ด์ ์ฌ์ฉํ ์ ์์ง๋ง, ๋ฐฐ์ด์ ๋ค๋ฅธ ํ์
์ ๋ค์ํ data๋ฅผ ๋ด๊ฒ๋๋ฉด ๊ฐ์ ์ ๊ทผํ๊ธฐ ์ํด ์ ํํ index๊ฐ์ ์์์ผ ํฉ๋๋ค.
โ๏ธ ๋ํ ๋ฐฐ์ด์ ๊ฐ ๊ฐ์ด ๋ฌด์์ ์๋ฏธํ๋์ง ์ ํํ ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค.
student1 = [1, "Kim", "4๋ฐ", "male", [100, 90, 92], "pass"] student2 = [2, "Lee", "2๋ฐ", "female", [70, 55, 32], "fail"] student3 = [3, "Park", "8๋ฐ", "female", [82, 45, 68], "fail"] student4 = [4, "Jang", "5๋ฐ", "male", [90, 85, 91], "pass"] console.log(student1[4]) // [ 100, 90, 92 ] console.log(student2[2]) // 2๋ฐ console.log(student3[5]) // fail console.log(student4[1]) // Jang
โ๏ธ ์ด๋ด ๋, ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด key๊ฐ์ผ๋ก ์ ๊ทผํ ์ ์๊ธฐ ๋๋ฌธ์ ์ง๊ด์ ์ด๊ณ ๊ฐ๊ฒฐํฉ๋๋ค.
students = { student1 : { number : 1, name : "Kim", classNumber : "4๋ฐ", sex : "male", score : { ๊ตญ์ด : 100, ์์ด : 90, ์ํ : 92 }, midtermExam : "pass" }, student2 : { number : 2, name : "Lee", classNumber : "2๋ฐ", sex : "female", score : { ๊ตญ์ด : 70, ์์ด : 55, ์ํ : 32 }, midtermExam : "fail" }, } console.log(students.student1.name) // Kim console.log(students.student2.midtermExam) // fail
โ๏ธ ๊ฐ์ฒด๋ ์์๋ ํค(Key)์ ๊ฐ(Value)์ ์์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. Python์ ๋์
๋๋ฆฌ์ ๋น์ทํฉ๋๋ค.
โ๏ธ ๋จ, Python์ ํค(Key) ๊ฐ์ strging ํํ์ง๋ง, JavaScript์์ ํค(Key) ๊ฐ์ string์ด์ง ์์๋ ๋ฉ๋๋ค.
let ๋ณ์๋ช = { Key1 : value, Key2 : value, Key3 : value, }
โ๏ธ ์ค๊ดํธ({}) ์์ key์ value๋ฅผ ์ ๋ ฅํ์ฌ ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ ๊ฒ์ "๊ฐ์ฒด ๋ฆฌํฐ๋ด ์ ์ธ ๋ฐฉ์"์ด๋ผ ํฉ๋๋ค.
let people = { firstName : "Jang", lastName : "Jaewon", age : 33, city : "Seoul", sex : "Male" }; console.log(person)
โ๏ธ new Object()๋ฅผ ๋ณ์์ ํ ๋นํ๊ณ , ๋ณ์์ ๊ฐ์ ํ ๋นํ์ฌ ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ ๋ฐฉ์์ "new Object ์ ์ธ ๋ฐฉ์"์ด๋ผ ํฉ๋๋ค.
let person = new Object(); person.firstName = "Jang"; person.lastName = "Jaewon"; person.age = 33; person.city = "Seoul"; person.sex = "Male"; console.log(person)
โ๏ธ ๊ฐ์ฒด์ property์ด๋ ๊ฐ์ฒด์ ์ํ๋ฅผ ๋ํ๋ด๋ ๊ฐ๋ฅผ ์๋ฏธํ๊ณ , method๋ ๊ธฐ๋ฅ์ด๋ ๋์์ ์ํํ ์ ์๋ ์์ฑ์ ์๋ฏธํฉ๋๋ค.
โ๏ธ ์ฆ, ๊ฐ์ฒด ๋ด ์์์ ๊ฐ์ด data์ผ ๊ฒฝ์ฐ์๋ property์ด๊ณ , ํจ์์ผ ๊ฒฝ์ฐ์๋ method๋ผ ํ ์ ์์ต๋๋ค.
let myself = { name: 'Jaewon', // ๐ property location : { // ๐ property country : 'South Korea', city : 'Seoul' }, age: 33, // ๐ property } console.log(myself.name) // Jaewon console.log(myself.location) // { country: 'South Korea', city: 'Seoul' } console.log(myself.location.country) // South Korea console.log(myself.location.city) // Seoul console.log(myself.age) // 33
let calculation = { plus : function(a,b){ // ๐ method return a+b }, minus : function(a,b){ // ๐ method return a-b } } console.log(calculation.plus(2,5)) // 7 console.log(calculation.minus(8,3)) // 11
let myself = { name: 'Jaewon', location : { country : 'South Korea', city : 'Seoul' }, age: 33, "favorit food" : ['๐', '๐', '๐ญ'], "shortCut" : { 1 : "VScode", 2 : "Chrome", 3 : "KakaoTale", } } console.log(myself.favorit food) // SyntaxError console.log(myself.shortCut.1) // SyntaxError console.log(myself.shortCut) // { '1': 'VScode', '2': 'Chrome', '3': 'KakaoTale' } console.log(myself.sc) // undefined
let myself = { name: 'Jaewon', location : { country : 'South Korea', city : 'Seoul' }, age: 33, "favorit food" : ['๐', '๐', '๐ญ'], "shortCut" : { 1 : "VScode", 2 : "Chrome", 3 : "KakaoTale", } } let sc = "shortCut" console.log(myself["favorit food"]) // [ '๐', '๐', '๐ญ' ] console.log(myself["shortCut"]["1"]) // VScode console.log(myself["shortCut"]) // { '1': 'VScode', '2': 'Chrome', '3': 'KakaoTale' } console.log(myself[sc]) // { '1': 'VScode', '2': 'Chrome', '3': 'KakaoTale' }
โ๏ธ ๊ฐ์ฒด์ ์กด์ฌํ์ง ์๋ key๊ฐ์ ๊ฐ์ ํ ๋นํ๋ฉด property๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค.
โ๏ธ ๊ฐ์ฒด์ ์กด์ฌํ๋ key๊ฐ์ ๋ค๋ฅธ ๊ฐ์ ํ ๋นํ๋ฉด property๋ฅผ ์์ ํ ์ ์์ต๋๋ค.
let myself = { name: 'Jaewon', location : { country : 'South Korea', city : 'Seoul' }, age: 33, "favorit food" : ['๐', '๐', '๐ญ'], "shortCut" : { 1 : "VScode", 2 : "Chrome", 3 : "KakaoTale", } } myself.hobby = 'coding' // ๐ ์กด์ฌํ์ง ์๋ key๊ฐ ์ถ๊ฐ myself.age = 50 // ๐ ์กด์ฌํ๋ key์ ์๋ก์ด ๊ฐ ํ ๋น console.log(myself)
โ๏ธ delete ๋ค์ ์ญ์ ํ ๊ฐ์ฒด์ key๋ฅผ ์ฃผ๋ฉด property๋ฅผ ์ญ์ ํ ์ ์์ต๋๋ค.
let myself = { name: 'Jaewon', location : { country : 'South Korea', city : 'Seoul' }, age: 33, "favorit food" : ['๐', '๐', '๐ญ'], "shortCut" : { 1 : "VScode", 2 : "Chrome", 3 : "KakaoTale", } } delete myself["favorit food"] delete myself["shortCut"]["3"] console.log(myself)