-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_destructuring.js
More file actions
138 lines (105 loc) · 2.85 KB
/
array_destructuring.js
File metadata and controls
138 lines (105 loc) · 2.85 KB
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
/**
* Introduction to JavaScript Array Destructuring
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment}
*/
// example 1
function getScores() {
return [55, 90, 100];
}
let scores = getScores();
let x = scores[0],
y = scores[1],
z = scores[2];
console.log(x, y, z); // 55, 90, 100
// alternative example 1 with destructuring
let [a, b, c] = getScores(); // [] look like the array syntax but they are not.
console.log(a, b, c); // 55, 90, 100
// example 2
function list() {
return [1, 2];
}
let [one, two, three] = list();
console.log(one, two, three); // 1, 2, undefined
// example 3
function list2() {
return [1, 2, 3];
}
let [one2, two2] = list2(); // remaining elements are discarded
console.log(one2, two2); // 1, 2
/**
* Array Destructuring with Rest Operator
*/
// example 4
function getScores2() {
return [55, 90, 99, 60, 77, 88];
}
let [x2, y2, ...rest] = getScores2();
console.log(x2, y2, rest); // 55, 90, [99, 60, 77, 88]
// example 5
let i, j;
[i, j] = [100, 200];
console.log(i, j); // 100, 200
/**
* Setting default values for array destructuring
*/
// example 6
function getItems() {
return ['Javascript', 'Node.js', 'Express.js'];
}
let items = getItems();
let fourthItem = items[3] !== undefined ? items[3] : 'Koa.js';
console.log(fourthItem); // Koa.js
let [item1, item2, item3, item4 = 'Koa.js'] = getItems();
console.log(item1, item2, item3, item4); // Javascript, Node.js, Express.js, Koa.js
// example 7
function notReturnArray() {
return null;
}
let [q = 1, w = 2] = notReturnArray() || []; // Error occurs if we don't set an empty array
console.log(q, w); // 1, 2
/**
* Nested Array Destructuring
*/
// example 8
function profile() {
return ['John', 'Doe', ['red', 'blue', 'green']];
}
let [fName, lName, [c1, c2, c3]] = profile();
console.log(fName, lName, c1, c2, c3); // John, Doe, red, blue, green
/**
* Array Destructuring assignment applications
*/
// example 9 (swap variables value without using a third variable)
let p = 100,
l = 200;
[p, l] = [l, p];
console.log(p, l); // 200, 100
// example 10 (functions that return multiple values)
function calculation(a, b) {
return [a + b, a - b, (a + b) / 2];
}
let [sum, difference, average] = calculation(20, 20);
console.log(sum, difference, average); // 40, 0, 20
/**
* Ignoring some returned values
*/
function f() {
return [1, 2, 3];
}
// [, ,] = f(); // ignored all values
let [first, , third] = f();
console.log(first, third); // 1, 3
/**
* Unpacking values from a regular expression match
*/
function parseProtocol(url) {
const parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
if (!parsedURL) {
return false;
}
const [, protocol, fullHost, fullPath] = parsedURL;
return protocol;
}
console.log(
parseProtocol('https://developer.mozilla.org/en-US/docs/Web/JavaScript')
); // https