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