๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DEV

JavaScript : Object

by camille: 2024. 10. 20.
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ Object

: ๊ฐ์ฒด, ์ฐธ์กฐํ˜• ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ํ•œ์ข…๋ฅ˜. ์—ฌ๋Ÿฌ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌถ์Œ์œผ๋กœ ๊ด€๋ฆฌํ•˜๋Š”
๋ฐ์ดํ„ฐ ํƒ€์ž…

object๋Š” ์ค‘๊ด„ํ˜ธ๋กœ ์‹œ์ž‘ํ•˜๊ณ  key๊ฐ€ ์žˆ๊ณ , key์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ, ๋˜ ๋‹ค๋ฅธ key๊ฐ€ ์žˆ๊ณ , ๋˜ ๋‹ค๋ฅธ key์— ํ•ด๋‹นํ•˜๋Š” ๋ฐ์ดํ„ฐ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

{name: 'Camille', isDeveloper: true}

๐Ÿ“— Array์™€ Object์˜ ๋น„๊ต

โœ” Array

let myself = [
  'Camille'
  'Republic of Korea'
  'Seoul'
  ['fashion', 'art']
]

โœ” Object

let myself = {
  name: 'Camille',
  location:{
  country: 'Republic of Korea',
  city: 'Seoul'},
  interest: ['fashion', 'art']
}

๐Ÿ“– object์—์„œ ๊ฐ์ฒด ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ property๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
์œ„ object์˜ˆ์‹œ์—์„œ ๋ณด๋ฉด myself๋ผ๋Š” ๊ฐ์ฒด์˜ property๋Š” 3๊ฐœ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
๋˜ ์ด property์—๋Š” key์™€ value๊ฐ€ ํฌํ•จ๋˜์–ด์žˆ๋Š”๋ฐ, name = 'Camille'์˜ property์—์„œ name์€ key๋ฅผ ๋œปํ•˜๋ฉฐ, 'Camille'์€ value๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

๐Ÿ“– ๋งŒ์ผ console.log(myself); ํ˜ธ์ถœํ•˜๊ฒŒ๋˜๋ฉด, key, value์˜ ์ง๋งŒ ๋งž์ถฐ์„œ ๋‚˜๊ณ ์˜ค ๊ฐ๊ฐ์˜ property์˜ index๋Š” ๋žœ๋ค์œผ๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

  console.log(myself);
< interest: ['fashion', 'art']
  location:{
  country: 'Republic of Korea',
  city: 'Seoul'},
  name: 'Camille',

๐Ÿ“˜ ๊ฐ์ฒด์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜๊ธฐ

๊ฐ์ฒด๋Š” key๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

let myself = {
  name: 'Camille',
  location:{
  country: 'Republic of Korea',
  city: 'Seoul'},
  interest: ['fashion', 'art']
}

๐Ÿ‘ฉโ€๐Ÿซ** 1. Dot Notation : .์„ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•**

myself.name//'Camille'
myself.interest//'fashion','art'

๐Ÿ‘ฉโ€๐Ÿซ** 2. Bracket Notation : []๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•**

myself['name']//'Camille'
myself['interes']//'fashion','art'
let myKey ='interest'
console.log(myself['interest'])
console.log(myself[mykey])
< ['fashion', 'art']
  ['fashion', 'art']
console.log(myself.mykey)
ํ•˜์ง€๋งŒ Dot Noyation์€ ๋ถˆ๊ฐ€๋Šฅ 

```javascript
์•„๋ž˜์™€ ๊ฐ™์ด key๋ถ€๋ถ„์„ string์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.

let myself = {
'name': 'Camille',
'location':{
'country': 'Republic of Korea',
'city': 'Seoul'},
'interest': ['fashion', 'art']
myKey : 'hello world'
}
let myKey ='interest'
console.log(myself['interest'])
console.log(myself[mykey])
console.log(myself.mykey)

<['fashion', 'art']
<['fashion', 'art']

Blacket Notation๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐพ์•„๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ์•ž์— ์ •์˜ํ•ด๋‘” 'interest'์— ๋‹ด๊ธด
๋‚ด์šฉ์„ ์ฝ˜์†”๋กœ ์ถœ๋ ฅ ํ•œ ๊ฒƒ์ด๋‹ค.

< 'hello world'

ํ•˜์ง€๋งŒ Dot Notation์—์„œ๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜ ๊ฐ’์ด ์•„๋‹Œ myself์— ํฌํ•จ๋œ
key์ธ myKey๋ฅผ ์ถœ๋ ฅํ•œ ๊ฒƒ์ด๋‹ค.
}

728x90
๋ฐ˜์‘ํ˜•