TIL07. JavaScript : About Object

ID์งฑ์žฌยท2021๋…„ 9์›” 6์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
19/19
post-thumbnail

๐Ÿ“Œ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š” JavaScript์˜ ๊ฐ์ฒด๋ฅผ ์“ฐ๋Š” ์ด์œ ์™€ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



๐ŸŒˆ About Object

๐Ÿ”ฅ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

๐Ÿ”ฅ "Bracket notation" & "Dot notation"

๐Ÿ”ฅ ๊ฐ์ฒด ๋‹ค๋ฃจ๊ธฐ(์ถ”๊ฐ€, ์ˆ˜์ •, ์‚ญ์ œ)



1. ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 


๐Ÿค” ๊ฐ์ฒด๋ฅผ ์™œ ์“ธ๊นŒ?

โœ”๏ธ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•  ๋•Œ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฐฐ์—ด์— ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๋‹ค์–‘ํ•œ 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,
}

๐Ÿค” "๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ์„ ์–ธ ๋ฐฉ์‹" & "new Object ์„ ์–ธ ๋ฐฉ์‹"

โœ”๏ธ ์ค‘๊ด„ํ˜ธ({}) ์•ˆ์— 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)

โœ”๏ธ ๊ฐ์ฒด์˜ 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


2. "Bracket notation" & "Dot notation"


๐Ÿค” Dot notation์€ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ํฌํ•จ๋œ key์—๋Š” ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํ• ๋‹น๋œ ๋ณ€์ˆ˜๋ฅผ key๋กœ ํ™œ์šฉํ•ด์„œ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

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

๐Ÿค” Bracket notation์€ ์ ‘๊ทผ์— ์žˆ์–ด ๋ณ„๋‹ค๋ฅธ ์ œ์•ฝ์ด ์—†์Šต๋‹ˆ๋‹ค.

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' }


3. ๊ฐ์ฒด ๋‹ค๋ฃจ๊ธฐ


๐Ÿค” ๊ฐ์ฒด property ์ถ”๊ฐ€ ๋ฐ ์ˆ˜์ • ๋ฐฉ๋ฒ•

โœ”๏ธ ๊ฐ์ฒด์˜ ์กด์žฌํ•˜์ง€ ์•Š๋Š” 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)

๐Ÿค” ๊ฐ์ฒด property ์‚ญ์ œ ๋ฐฉ๋ฒ•

โœ”๏ธ 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)
profile
Keep Going, Keep Coding!

0๊ฐœ์˜ ๋Œ“๊ธ€