Skip to content

Commit 427517d

Browse files
committed
Update chapter_36, chapter_37
1 parent 84c9f92 commit 427517d

File tree

2 files changed

+612
-0
lines changed

2 files changed

+612
-0
lines changed

book/이용우/chapter_36.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
### Chapter 36 - 디스트럭처링 할당
2+
3+
디스트럭처링 할당(Destructuring assignment)은 이터러블 또는 객체를 비구조화(Destructuring)하여
4+
한개 이상의 변수에 개별적으로 할당하는 것을 의미한다.
5+
6+
> 이터러블 또는 객체 리터럴에서 필요한 값을 추출하여 변수에 할당할 때 주로 사용된다.
7+
8+
<br>
9+
10+
### 배열 디스트럭처링 할당
11+
배열 디스트럭처링 할당은 배열의 각 요소를 배열로부터 추출하여 하나 이상의 변수에 할당한다.
12+
할당 대상은 이터러블해야하고, 할당 기준은 배열의 index이다.
13+
14+
``` js
15+
const arr = [1, 2, 3];
16+
17+
// 디스트럭처링 할당
18+
const [one, two, three] = arr;
19+
console.log(one, two, three); // 1 2 3
20+
```
21+
22+
<br>
23+
24+
변수의 개수와 이터러블의 요소 개수가 반드시 일치할 필요는 없다.
25+
26+
``` js
27+
const [a, b] = [1, 2];
28+
console.log(a, b); // 1 2
29+
30+
const [c, d] = [1];
31+
console.log(c, d); // 1 undefined
32+
33+
const [e, f] = [1, 2, 3];
34+
console.log(e, f); // 1 2
35+
```
36+
37+
<br>
38+
39+
배열 디스트럭처링 할당은 배열과 같은 이터러블에서 필요한 요소만 추출하여 변수에 할당하고 싶을 때 주로 사용한다.
40+
``` js
41+
function parseURL(url = '') {
42+
const parsedURL = url.match(/^(w+):\/\/([^/]+)(.*)$/);
43+
if (!parsedURL) return {};
44+
45+
// 배열 디스트럭처링 할당을 사용하여 이터러블에서 필요한 요소만 추출한다.
46+
const [, protocol, host, path] = parsedURL;
47+
return { protocol, host, path };
48+
}
49+
50+
const parsedURL = parseURL('https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array');
51+
console.log(parsedURL);
52+
// { protocol: 'https', host: 'developer.mozilla.org', path: '/ko/docs/Web/JavaScript/Reference/Global_Objects/Array' }
53+
```
54+
55+
<br><br>
56+
57+
### 객체 디스트럭처링 할당
58+
객체의 각 프로퍼티를 객체로부터 추출하여 1개 이상의 변수에 할당한다.
59+
이때 객체 디스트럭처링 할당의 대상은 객체이며, **할당 기준은 프로퍼티의 키이다.**
60+
61+
``` js
62+
const user = { firstName: 'firstName', lastName: 'lastName' };
63+
64+
// *프로퍼티 키를 기준*으로 디스트럭처링 할당이 이루어진다. 순서는 의미가 없다.
65+
const { lastName, firstName } = user;
66+
console.log(lastName, firstName); // lastName firstName
67+
```
68+
69+
<br>
70+
71+
만약 객체의 프로퍼티 키와 다른 변수 이름으로 프로퍼티 키를 할당하고 싶다면 아래와 같이 변수를 선언하면 된다.
72+
73+
``` js
74+
const user = { firstName: 'firstName', lastName: 'lastName' };
75+
76+
// 프로퍼티 키와 다른 변수 이름으로 프로퍼티 키를 할당하고 싶다면 아래와 같이 변수를 선언하면 된다.
77+
const { lastName: lastName2, firstName: firstName2 } = user;
78+
console.log(lastName2, firstName2); // lastName firstName
79+
```
80+
81+
82+
<br>
83+
84+
객체 디스트럭처이 할당은 객체에서 프로퍼티 키로
85+
필요한 프로퍼티 값만 추출하여 변수에 할당하고 싶을 때 유용하다.
86+
87+
``` js
88+
const str = 'Hello';
89+
90+
const { length } = str;
91+
console.log(length); // 5
92+
93+
const todo = { id: 1, content: 'HTML', completed: true };
94+
95+
// todo 객체에서 id만 추출한다.
96+
const { id } = todo;
97+
console.log(id); // 1
98+
```
99+
100+
<br>
101+
102+
객체 디스트럭처링 할당은 함수의 매개변수에서도 활용할 수 있다.
103+
104+
``` js
105+
function printTodo({ content, completed }) {
106+
console.log(`${content} is ${completed ? 'completed' : 'not completed'}`);
107+
}
108+
109+
printTodo({ id: 1, content: 'HTML', completed: true });
110+
// HTML is completed
111+
```
112+
113+
<br><br>
114+
115+
배열의 요소가 객체인 경우 배열 디스트럭처링 할당과 객체 디스트럭처링 할당을 혼용할 수 있다.
116+
117+
``` js
118+
const todos = [
119+
{ id: 1, content: 'HTML', completed: true },
120+
{ id: 2, content: 'CSS', completed: false },
121+
{ id: 3, content: 'JavaScript', completed: false }
122+
];
123+
124+
125+
// todos 배열의 두 번째 요소인 객체로부터 id 프로퍼티만 추출한다.
126+
const [, { id }] = todos;
127+
console.log(id); // 2
128+
```
129+
130+
<br>
131+
132+
중처버 객체의 경우에는 아래와 같이 사용 가능하다.
133+
134+
``` js
135+
const user = {
136+
name: 'Lee',
137+
address: {
138+
city: 'Seoul',
139+
country: 'Korea'
140+
}
141+
};
142+
143+
// address 프로퍼티 키로 객체를 추출하고 이 객체의 city 프로퍼티 키로 값을 추출한다.
144+
const { address: { city } } = user;
145+
console.log(city); // Seoul
146+
```
147+
148+
<br><br>
149+
150+
객체 디스트럭처링 할당을 위한 변수에 Rest 프로퍼티 `...`를 사용할 수 있다.
151+
152+
``` js
153+
// Rest 프로퍼티
154+
const { x, ...rest } = { x: 1, y: 2, z: 3 };
155+
console.log(x, rest); // 1, {y: 2, z: 3}
156+
```
157+
158+
159+
끝.

0 commit comments

Comments
 (0)