File tree Expand file tree Collapse file tree 1 file changed +22
-10
lines changed Expand file tree Collapse file tree 1 file changed +22
-10
lines changed Original file line number Diff line number Diff line change 1
- //console.log(person.name); ์ผ๋ก person.name, ๊ฐ์ฒด ํ์
์ผ๋ก ์ ๊ทผํ๊ธฐ ์ํ object ํ์
์ ์ ๋ฐฉ๋ฒ
2
- const person : {
3
- //entry ์ถ๊ฐ
4
- //์์ ๋งํ ๋ฏ ๊ฐ์ฒด ํ์
์ ์ ์ํ๊ธฐ ์ํด์๋ ํ ๋น ํ `;`๋ฅผ ๋ถ์ฌ์ผ ํ๋ค.
5
- name : string ;
6
- age : number ;
7
- // ์ด๋ ๊ฒ ์์ฑ์ ํ๊ฒ ๋๋ฉด ์ถํ์ ์ ์ํ ๊ฐ์ฒด ํ์
์ ์ฌํ์ฉํ ์ ์๊ฒ ๋๋ค.
8
- } = { //`{}` ํน์ ๊ฐ์ฒด ํ์
์ ์ํ ํ๊ธฐ๋ฒ, ๊ฐ์ฒด์ ๊ตฌ์กฐ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ๊ฒ. ๋น `{}`์ผ๋ก ํ ๋น์ ํ๋ฉด ๊ฐ์ฒด๋ก ํ ๋นํ๋ ๊ฒ๊ณผ ๋์ผ์ํ๋ค.
1
+ const person = {
9
2
name : 'Maximilian' ,
10
- age : 30
3
+ age : 30 ,
4
+ hobbies : [ 'Sports' , 'Cooking' ]
11
5
} ;
12
6
13
- console . log ( person . name ) ; //error : Object์ name์ด ์์ต๋๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ป๊ฒ ํด์ผํ ๊น?
7
+ let favoriteActivities : string [ ] ;
8
+ favoriteActivities = [ 'Sports' ] ;
9
+
10
+ console . log ( person . name ) ;
11
+
12
+ //hobby: ๋ฌธ์์ด
13
+ for ( const hobby of person . hobbies ) {
14
+ //์ฌ๊ธฐ์ hobby๋ ๋ฌธ์์ด๋ก ์ทจ๊ธ์ด ๋๊ณ ์๋ค.
15
+ //์ด์ ๋ ํ์
์ถ๋ก ์ผ๋ก ์ธํด์๋ค. person.hobbies๋ ๋ฌธ์์ด ๋ฐฐ์ด์ด๋ผ๊ณ ์ธ์ํ๊ณ ์๊ธฐ ๋๋ฌธ์ด๋ค.
16
+ console . log ( hobby ) ;
17
+ //ํ์ง๋ง map์ผ๋ก ์ ๊ทผ์ ํ๊ฒ ๋๋ฉด error๊ฐ ๋ฐ์ํ๋ค. ์ด์ ๋ ๋ฌธ์์ด์ map์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
18
+ // console.log(hobby.map(el => console.log(el)));// ๋ง์ฝ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด person.hobbies๋ฅผ ๋ค๋ฅธ ๋ณ์๋ก ์ ์ํ๊ณ map์ ์ฌ์ฉํ๋ฉด ๋ฌธ์ ์์ด ์ฌ์ฉํ ์๋ค.
19
+ }
20
+
21
+ //arr: ๋ฌธ์์ด ๋ฐฐ์ด
22
+ let hobby = person . hobbies ;
23
+ // let arr = person.hobbies;
24
+ let arr = hobby ;
25
+ arr . map ( el => console . log ( el ) ) ;
You canโt perform that action at this time.
0 commit comments