[JavaScript] ๊ฐ์ฒด(Object)

๋””๋”ฉยท2022๋…„ 11์›” 4์ผ
0

Section

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

๐Ÿ•– DAY +12 / 2022. 11. 04

SEB FE / Section1 / Unit9 / JavaScript ๋ฐฐ์—ด, ๊ฐ์ฒด



๐Ÿ“Œ Today I Learned

  • ๊ฐ์ฒด(Object)

๐Ÿ’ฌ ๊ฐ์ฒด

ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ์ ธ ์žˆ์„ ๋•Œ ์ ํ•ฉํ•œ ์ž๋ฃŒ ๊ตฌ์กฐ
๊ฐ์ฒด ์„ ์–ธ์„ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ๋‹ค
๊ฐ์ฒด๋Š” ํ‚ค์™€ ๊ฐ’ ์Œ(key-value pair)์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค

let user = {
	firstName: 'meoing',
    lastName: 'choi',
    email: 'meoing@velog.com',
    city: 'Seoul'
};


๐Ÿ’ฌ ๊ฐ์ฒด์˜ ๊ฐ’ ์‚ฌ์šฉํ•˜๊ธฐ

๊ฐ์ฒด์˜ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

๐Ÿ“‚ Dot notation

  • .์€ ๊ฐœ์ฒด์˜ ์†์„ฑ์„ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ (์œ ์ €์˜ ์†์„ฑ)
let user = {
	firstName: 'meoing',
    lastName: 'choi',
    email: 'meoing@velog.com',
    city: 'Seoul'
};

user.firstName;
// 'meoing'

user.city;
// 'Seoul'

๐Ÿ“‚ Bracket notation

๋™์ ์ธ ๊ฐ’์„ ํ• ๋‹นํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
ํ‚ค๊ฐ’์ด ๋™์ ์œผ๋กœ ๋ณ€ํ•  ๋•Œ, ํ‚ค๊ฐ’์ด ๋ณ€์ˆ˜์ผ ๋•Œ Bracket notation์„ ์“ด๋‹ค.

let user = {
	firstName: 'meoing',
    lastName: 'choi',
    email: 'meoing@velog.com',
    city: 'Seoul'
};

user['firstName'];
// 'meoing'

user['city']
// 'Seoul'

๐Ÿ“‚ ์˜ˆ์ œ

let tweet = {
	writer: 'monge',
    createdAt: '2022-11-03 20:38:33',
    content: '๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!'
}

// monge๋ผ๋Š” ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ ์œ ์ €๊ฐ€ SNS์— ์ƒˆ๋กœ์šด ๊ธ€์„ ์˜ฌ๋ ธ๋‹ค. 
// ๊ทธ๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€ "๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!"๋ผ๋Š” ๋‚ด์šฉ์„ bracket notation์œผ๋กœ ์–ด๋–ป๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์„๊นŒ?
/* dot notation */
tweet.writer
// "monge"
tweet.content
// "๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!"

/* braket notation */
tweet['writer'];
// "monge"
tweet['content'];
// "๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!"



๐Ÿ’ฌ ๊ฐœ๋…ํ•™์Šต

tweet['content']
// "๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!"
// ํ‚ค-๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค

tweet[content]
// ReferenceError: content is not defined
// ''๋ฅผ ๊ฑท์–ด๋‚ด๋‹ˆ ํ‚ค-๊ฐ’์ด ์•„๋‹ˆ๋ผ ๋ณ€์ˆ˜์˜ ๊ฐ’์œผ๋กœ ์ธ์‹ ์ค‘

// ๊ฐ์ฒด์— ํ•ญ์ƒ ๋ฌธ์ž์—ด๋กœ ๋„˜๊ฒจ ์ฃผ๊ฑฐ๋‚˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
let person = {
	name: 'meoing',
    age: 20
};

function getProperty(obj, propoertyName) {
	return obj[propoertyName];
}

// 1
let output = getProperty(person, 'name');
console.log(output); // --> 'meoing'

// 2
let output2 = getProperty(person, 'age');
console.log(output2); // --> '20'

//

๐Ÿ“‚ ๊ฐ์ฒด ์•ˆ ๊ฐ’ ์ถ”๊ฐ€

tweet['category'] = '์žก๋‹ด';
tweet.isPublic = true;
tweet.tags = ['#ํŠธ์œ—', '#๋ฐ์ผ๋ฆฌ'];

๐Ÿ“‚ ๊ฐ์ฒด ์•ˆ ๊ฐ’ ์‚ญ์ œ

let tweet = {
	writer: 'monge',
    createdAt: '2022-11-03 20:38:33',
    content: '๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!'
}

// createdAt ํ‚ค-๊ฐ’ ์Œ์„ ์ง€์šด๋‹ค
delete tweet.createdAt;

// tweet์€ ๋‹ค์Œ๊ณผ ๊ฐ™๊ฒŒ ๋œ๋‹ค
// { writer: 'monge', content: '๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!'

๐Ÿ“‚ in ์—ฐ์‚ฐ์ž

๊ฐ์ฒด ์•ˆ์— ํ•ด๋‹น ํ‚ค-๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค.

let tweet = {
	writer: 'monge',
    createdAt: '2022-11-03 20:38:33',
    content: '๋ฐฐ๊ฐ€ ๋ถ€๋ฅด๋‹ค!'
}

'content' in tweet;
// true

'updatedAt' in tweet;
// false
profile
๋ฌด์‚ฌ์™€์š”... ์—ด๊ณตํ–์š”...

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