-
Notifications
You must be signed in to change notification settings - Fork 0
/
destructuring.js
217 lines (173 loc) · 3.78 KB
/
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
//////////////////////////
// ES6 - Destructuring
/////////////////////////
/*
// ES5
var expense = {
type: 'Business',
amount: '$45 USD'
};
//var type = expense.type;
//var amount = expense.amount;
// ES6 Option 1
//const { type } = expense;
//const { amount } = expense;
// ES6 Option 2
const { type, amount } = expense;
console.log(type);
console.log(amount);
*/
// ===================================================
/*
var savedFile = {
extension: 'jpg',
name: 'report',
size: 14040
};
function fileSummary({ name, extension, size }, { color }) {
return `The ${color} file ${name}.${extension} is of size ${size} kb`;
}
console.log(fileSummary(savedFile, { color: 'red' }));
*/
// ============================================
/*
const companies = [
'Google',
'Facebook',
'Uber'
];
// ES6
const [ name, name2, name3, name4 ] = companies
console.log(name);
console.log(name2);
console.log(name3);
console.log(typeof name4);
// ES5
const firstCompany = companies[0];
console.log(firstCompany);
// ES6
const [ name5 ] = companies;
console.log(name5);
// ES6
const { length } = companies;
console.log(length);
// => 3
const [ name6, name7, ...rest ] = companies;
console.log(name6);
console.log(name7);
console.log(...rest);
*/
// =========================================
/*
const companies = [
{ name: 'Google', location: 'Mountain View' },
{ name: 'Facebook', location: 'Menlo Park' },
{ name: 'Uber', location: 'San Francisco' }
];
// ES5
var locationGoogle = companies[0].location;
console.log(locationGoogle);
// ES6
const [{ location }] = companies;
console.log(location);
*/
// ================================================
/*
const Google = {
locations: ['Mountain View', 'New York', 'London']
};
const { locations } = Google;
console.log(locations);
const { locations: [ location ] } = Google;
console.log(location);
*/
// =================================================
/*
function signup({ username, password, email, dateOfBirth, city }) {
// create new user
}
// other code
// other code
// other code
// other code
// other code
// other code
// other code
// other code
const user = {
username: 'myname',
password: 'mypassword',
email: 'isak@testing.com',
dateOfBirth: '21/04/1982',
city: 'City of God'
};
signup(user);
*/
// ===================================
/*
const points = [
[4, 5],
[10, 1],
[0, 40]
];
/* ==> convert to:
[
{ x: 4, y: 5 },
{ x: 10, y: 1 },
{ x: 0, y: 40 }
]
*/
/*
var convertion = points.map(([ x, y ]) => {
return { x, y };
});
console.log(convertion);
*/
// ===================================================
/*
// Destructuring in Practice
const profile = {
title: 'Engineer',
department: 'Engineering'
};
// ES5
function isEngineer(profile) {
var title = profile.title;
var department = profile.department;
return title === 'Engineer' && department === 'Engineering';
}
// ES6
function isEngineer({ title, department }) {
return title === 'Engineer' && department === 'Engineering';
}
*/
// ==================================
/*
//Array Destructuring in Practice
const classes = [
[ 'Chemistry', '9AM', 'Mr. Darnick' ],
[ 'Physics', '10:15AM', 'Mrs. Lithun'],
[ 'Math', '11:30AM', 'Mrs. Vitalis' ]
];
const classesAsObject = classes.map(([ subject, time, teacher ]) => {
return { subject, time, teacher };
});
console.log(classesAsObject);
*/
// ===================================
/*
// Recursion with Destructuring
const numbers = [1, 2, 3];
function double([first, ...rest], accum = []) {
if (rest.length) {
accum.push(first * 2);
return double(rest, accum);
} else {
accum.push(first * 2);
return accum;
}
}
function double([number, ...rest]) {
return !number ? [] : [number * 2, ...double(rest)];
}
*/