๐ค๊ฐ์ฒด(Objects) - โ๋ค์ํ ํํ์ ์๋ฃํโ์ ๊ฐ์ง ์๋ฃ๊ตฌ์กฐ
๊ฐ์ฒด : 'ํค'์ '๊ฐ'์ผ๋ก ์ด๋ฃจ์ด์ง 'ํ๋กํผํฐ'๋ค์ ์งํฉ! (ํค์ ๊ฐ์ ์งํฉ)
๊ฐ์ฒด์ ๋ชฉ์ : ๋ค์ํ "๋ฐ์ดํฐ ํ์ "์ ์๋ฃ๋ค์ ํ ๋นํ๊ธฐ ์ํจ
ํ๋กํผํฐ ํค : (๋ฌธ์์ด)
ํ๋กํผํฐ ๊ฐ : (๋ชจ๋ ๊ฐ)
ํ๋กํผํฐ ๊ฐ์ด ํจ์์ธ ๊ฒฝ์ฐ๋ฅผ "๋ฉ์๋"
๋ฉ์๋ : ๊ฐ์ฒด์์ ํจ์
๊ฐ์ฒด์์ ํ๋กํผํฐ ๊ฐ์ด ํจ์์ธ : ๋ฉ์๋
ํจ์ ์์ ๋ฉ์๋๊ฐ ํฌํจ!!
|๊ฐ์ฒด์ ์ ์์ ํน์ง
- ๊ฐ์ฒด๋ ์์ฑ(property)์ ์งํฉ์ด๋ฉฐ, ์ค๊ดํธ ๋ด๋ถ์ key์ value์ ์์ผ๋ก ํํ
- key๋ ๋ฌธ์์ด ํ์
*๋ง ๊ฐ๋ฅ
- (์ฐธ๊ณ ) key ์ด๋ฆ์ ๋์ด์ฐ๊ธฐ ๋ฑ์ ๊ตฌ๋ถ์๊ฐ ์์ผ๋ฉด ๋ฐ์ดํ๋ก ๋ฌถ์ด์ ํํ
- value๋ ๋ชจ๋ ํ์ (ํจ์ํฌํจ) ๊ฐ๋ฅ
- ๊ฐ์ฒด ์์ ์ ๊ทผ์ ์ ๋๋ ๋๊ดํธ๋ก ๊ฐ๋ฅ
- (์ฐธ๊ณ ) key ์ด๋ฆ์ ๋์ด์ฐ๊ธฐ ๊ฐ์ ๊ตฌ๋ถ์๊ฐ ์์ผ๋ฉด ๋๊ดํธ ์ ๊ทผ๋ง ๊ฐ๋ฅ
const me = {
name: 'jack',
phoneNumber: '01012345678',
'samsung products': {
buds: 'Galaxy Buds pro',
galaxy: 'Galaxy s20โ,
},
}
console.log(me.name)
console.log(me.phoneNumber)
console.log(me['samsung products'])
console.log(me['samsung products'].buds)
|๊ฐ์ฒด์ ๋ฉ์๋; ๋ฉ์๋(๊ฐ์ฒด "์์" ์๋ ํจ์, ํจ์!=๋งค์๋)
- ๋ฉ์๋๋ ๊ฐ์ฒด์ ์์ฑ์ด ์ฐธ์กฐํ๋ ํจ์.
- ๊ฐ์ฒด.๋ฉ์๋๋ช () ์ผ๋ก ํธ์ถ ๊ฐ๋ฅ.
- ๋ฉ์๋ ๋ด๋ถ์์๋ this ํค์๋๊ฐ ๊ฐ์ฒด๋ฅผ ์๋ฏธํจ.
- fullName์ ๋ฉ์๋๊ฐ ์๋๊ธฐ ๋๋ฌธ์ ์ ์ ์ถ๋ ฅ ๋์ง ์์(NaN)
- getFullName์ ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์ ํด๋น. ๊ฐ์ฒด์ firstName ๊ณผ lastName์ ์ ์์ ์ผ๋ก ์ด์ด์ ๋ฐํ
const me = {
firstName: 'John',
lastName: 'Doeโ,
fullName: this.firstName + this.lastName,
getFullName: function () {
return this.firstName + this.lastName
}
|๊ฐ์ฒด ๊ด๋ จ ES6 ๋ฌธ๋ฒ
- ์์ฑ๋ช ์ถ์ฝ
- ๋ฉ์๋๋ช ์ถ์ฝ
- ๊ณ์ฐ๋ ์์ฑ๋ช ์ฌ์ฉํ๊ธฐ
- ๊ตฌ์กฐ ๋ถํด ํ ๋น*
- (์ฐธ๊ณ ) ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ๋ฐฐ์ด๋ ๊ฐ๋ฅํจ
- ๊ฐ์ฒด ์ ๊ฐ ๊ตฌ๋ฌธ(Spread Operator)
|์์ฑ๋ช ์ถ์ฝ (shorthand)
- ๊ฐ์ฒด๋ฅผ ์ ์ํ ๋ key์ ํ ๋นํ๋ ๋ณ์์ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์์์ ๊ฐ์ด ์ถ์ฝ ๊ฐ๋ฅ
var books = ['Learning JS', 'Learning Python']
var magazines = ['Vogue', 'Science']
// ES5
var bookShop = {
books: books,
magazines: magazines,
}
console.log(bookShop)
////////////////////////////////////////////////////////
const books = ['Learning JS', 'Learning Python']
const magazines = ['Vogue', 'Science']
// ES6+
const bookShop = {
books,
magazines,
}
console.log(bookShop)
/*
{
books: ['Learning JS', 'Learning Python'],
magazines: ['Vogue', 'Science']
}
*
|๋ฉ์๋๋ช ์ถ์ฝ (shorthand)
- ๋ฉ์๋ ์ ์ธ ์ function ํค์๋ ์๋ต ๊ฐ๋ฅ
// ES5
var obj = {
greeting: function () {
conosle.log('Hi!')
}
}
obj.greeting() // Hi!
///////////////////////////////////
// ES6+
const obj = {
greeting() {
console.log('Hi!')
}
}
obj.greeting() // Hi
|๊ณ์ฐ๋ ์์ฑ(computed property name)
- ๊ฐ์ฒด๋ฅผ ์ ์ํ ๋ key์ ์ด๋ฆ์ ํํ์์ ์ด์ฉํ์ฌ ๋์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅ
const key = 'regions'
const value = ['๊ด์ฃผ','๋์ ','๊ตฌ๋ฏธ','์์ธ']
const ssafy = {
[key]:value,
}
console.log(ssafy) // {regions : Array(4)}
console.log(ssafy.regions) //["๊ด์ฃผ","๋์ ","๊ตฌ๋ฏธ","์์ธ"]
|๊ตฌ์กฐ ๋ถํด ํ ๋น (destructing assignment)
- ๋ฐฐ์ด ๋๋ ๊ฐ์ฒด๋ฅผ ๋ถํดํ์ฌ ์์ฑ์ ๋ณ์์ ์ฝ๊ฒ ํ ๋นํ ์ ์๋ ๋ฌธ๋ฒ
const userInformation = {
name:'chun sik',
userId: 'sweetpotato',
phoneNumber: '010-000-0000',
email: 'chunsik@sweetpotato.com'
}
const name = userInformation.name
const userId = userInformation.userId
const phoneNumber = userInformation.phoneNumber
const email = userInformation.email
/////////////////////////////////////////
const userInformation = {
name:'chun sik',
userId: 'sweetpotato',
phoneNumber: '010-000-0000',
email: 'chunsik@sweetpotato.com'
}
const {name} = userInformation
const {userId} = userInformation
const {phoneNumber} = userInformation
const {email} = userInformation
//์ฌ๋ฌ๊ฐ๋ ๊ฐ๋ฅ!
const {name, userId} = userInformation
|Spread Operator
- spread operator(...)๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ฒด ๋ด๋ถ์์ ๊ฐ์ฒด ์ ๊ฐ ๊ฐ๋ฅ
- ์์ ๋ณต์ฌ์ ํ์ฉ ๊ฐ๋ฅ
const obj = {b: 2, c: 3, d: 4}
const newObj = {a: 1, ...obj, e: 5}
console.log(newObj) // {a: 1, b: 2, c: 3, d: 4, e: 5
|JSON(JavaScriptObjectNotation)
- key-value์์ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ํ๊ธฐํ๋ ์ธ์ด ๋ ๋ฆฝ์ ํ์ค ํฌ๋งท
- ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด์ ์ ์ฌํ๊ฒ ์๊ฒผ์ผ๋ ์ค์ ๋ก๋ ๋ฌธ์์ด ํ์
- ๋ฐ๋ผ์ JS์ ๊ฐ์ฒด๋ก์จ ์กฐ์ํ๊ธฐ ์ํด์๋ ๊ตฌ๋ฌธ ๋ถ์(parsing)์ด ํ์
- ์๋ฐ์คํฌ๋ฆฝํธ์์๋ JSON์ ์กฐ์ํ๊ธฐ ์ํ ๋ ๊ฐ์ง ๋ด์ฅ ๋ฉ์๋๋ฅผ ์ ๊ณต
- JSON.parse()
- JSON => ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
- JSON.stringify()
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด => JSON
- JSON.parse()
//Object -> JSON
const jsonData = JSON.stringify({
coffee: 'Americano',
iceCream: 'Cookie and cream',
})
console.log(jsonData) // "{"coffe":"Americano",...
console.log(typeof jsonData) //string
//JSON -> Object
const jsonData = JSON.stringify({
coffee: 'Americano',
iceCream: 'Cookie and cream',
})
const parseData = JSON.parse(jsonData)
console.log(parseData) // {coffee:"Americano",...
console.log(typeof parseData) //object
'Language > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript - ํ๋กํ ํ์ (prototype), ํด๋์ค(class) (0) | 2022.05.03 |
---|---|
JavaScript - this (0) | 2022.05.02 |
JavaScript - ๋ฐฐ์ด(Arrays) (0) | 2022.04.27 |
JavaScript - ๋ฌธ์์ด(String) (0) | 2022.04.26 |
JavaScript - ํจ์(function) (0) | 2022.04.26 |
๋๊ธ