JavaScript : Object

camille·2022년 4월 7일
0

JavaScript

목록 보기
6/7
post-thumbnail

📌 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은 불가능 
아래와 같이 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를 출력한 것이다.
}

0개의 댓글