-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
object-destructuring.js
259 lines (211 loc) · 5.25 KB
/
object-destructuring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// 1. Destructure and pick a property
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { name } = user;
console.log(name);
}
// 2. Declaration Gotcha
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
{ name } = user // Uncaught SyntaxError: Unexpected token '='
let name;
{ name } = user; // user // Uncaught SyntaxError: Unexpected token '='
({ name } = user)
console.log(name); // Alex
}
// 3. Desctructure and pick multiple properties
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { name, age } = user;
console.log(name, age);
}
// 4. Assign new variable and default value
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const {name, age, salary=123455} = user;
console.log(name, age, salary)
}
// 5. Destructure and add aliases
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const { address: permanentAddress } = user;
console.log(permanentAddress);
}
// 6. Destructuring nested objects
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43,
'department':{
'name': 'Sales',
'Shift': 'Morning',
'address': {
'city': 'Bangalore',
'street': '7th Residency Rd',
'zip': 560001
}
}
}
const { department } = user; // {name: "Sales", Shift: "Morning", address: {…}}
const { department: { address } } = user; // {city: "Bangalore", street: "7th Residency Rd", zip: 560001}
const { department: { address: { city } } } = user; // Bangalore
}
// 7. Dynamic name property
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const getValue = key => {
const { [key]: returnValue } = user;
return returnValue;
}
getValue('name') // Alex's
getValue('age') // 43
}
// 8. Destructuring into Function Parameter
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
logDetails(user); // Alex is 43 year(s) old!
function logDetails({name, age}) {
console.log(`${name} is ${age} year(s) old!`)
}
}
// 9. Destructure function return
{
const getUser = () => {
return{
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
}
const { name, address, age } = getUser();
console.log(name, age, address);
}
// 10. Destructuring in loops
{
const users = [{
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
},
{
'name': 'Bob',
'address': 'Canada',
'age': 53
},
{
'name': 'Carl',
'address': 'Bangalore',
'age': 26
}];
for(let { name, age } of users) {
console.log(name)
}
}
// 11. Destructure console.log
{
const { log, warn, error } = console;
}
// 12. Create a clone of an object
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const clone = {...user} // {name: "Alex", address: "15th Park Avenue", age: 43}
clone === user; // false
}
// 13. Add properties to an object in Immutable way
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const updatedUser = {...user, salary:12345}; // {name: "Alex", address: "15th Park Avenue", age: 43, salary: 12345}
console.log(user); // {name: "Alex", address: "15th Park Avenue", age: 43}
}
// 14. Update an object in Immutable way
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const updatedUser = {...user, age:56}; // {name: "Alex", address: "15th Park Avenue", age: 56}
console.log(user); // {name: "Alex", address: "15th Park Avenue", age: 43}
}
// 15. Update Nested objects
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43,
'department':{
'name': 'Sales',
'Shift': 'Morning',
'address': {
'city': 'Bangalore',
'street': '7th Residency Rd',
'zip': 560001
}
}
}
{...user, department: {'number': 006}} // department replaced
{...user, department: {...user.department, 'number': 006}} // Right way
}
// 16. Combine two objects - Shallow Merge
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const department = {
'name': 'Sales',
'Shift': 'Morning'
}
const completeDetails = {...user, ...department};
console.log({completeDetails});
}
// 17. Rest operator in Object Destructuring
{
const user = {
'name': 'Alex',
'address': '15th Park Avenue',
'age': 43
}
const {name, ...rest} = user;
console.log(name, rest);
const {age, ...rest} = user;
console.log(age, rest);
}