-
Notifications
You must be signed in to change notification settings - Fork 0
/
15_destructuring.js
155 lines (116 loc) · 4.66 KB
/
15_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
/*----------------------------------------------------------------------------------------------*/
/* Destructuring is great for setting multiple object properties to variables at the same time.
------------------------------------------------------------------------------------------------*/
const me = {
name: "Dmytro Anikin",
nickname: "Dimianni",
age: 20,
tasks: ["learn JS", "exercise"]
}
/*--------------------------------------------------------*/
/* Instead of this:
----------------------------------------------------------*/
// const name = me.name
// const nickname = me.nickname
// const age = me.age
/*--------------------------------------------------------*/
/* Can do this:
----------------------------------------------------------*/
// NOTE: in object destructuring order does not matter
// variable names and the object keys are being matched to assign the correct value!
// To define variables with names different from keys, use "name: fullname", where
// 'name' is object key and 'fullname' is the variable name
const { name: fullname, nickname, age, location = "Aberdeen" } = me;
// console.log(fullname, nickname, age, location);
/*--------------------------------------------------------*/
/* Another example
----------------------------------------------------------*/
const me2 = {
name: "Dima",
surname: "Anikin",
nick: "Dimianni",
hobbies: ["surf", "code", "travel"],
}
let {
name,
surname,
hobbies,
hobbies: [surf, code, travel]
} = me2;
// console.log(name, surname, hobbies, surf, code, travel);
/*----------------------------------------------------------------------------------------------*/
/* Can also destructure object's properties when passing it into a function
------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------*/
/* Instead of this:
----------------------------------------------------------*/
// function logTasks(obj) {
// obj.tasks.forEach(function (task) {
// console.log(`${obj.name} has to ${task}`);
// })
// }
/*--------------------------------------------------------*/
/* Can do this:
----------------------------------------------------------*/
// I can also set a new name for the object key.
// And even set a default value for a key that does not exist (otherwise it will return undefined)
const logTasks = ({ name:firstName, tasks, country = "US" }) => {
tasks.forEach(task => {
console.log(`${firstName}, from the ${country}, has to ${task}`);
})
}
// logTasks(me);
/*----------------------------------------------------------------------------------------------*/
/* Nested destructuring
------------------------------------------------------------------------------------------------*/
const student = {
firstname: 'Dimon',
lastname: 'Anikin',
ielts_scores: {
speaking: 8.0,
listening: 8.5,
writing: 9.5,
reading: 8.0
}
};
const {
firstname,
lastname,
country = "Ukraine",
ielts_scores: { speaking, listening, writing, reading }
} = student;
// console.log(country, speaking, listening, writing, reading);
/*----------------------------------------------------------------------------------------------*/
/* Same goes for setting variables for array elements
------------------------------------------------------------------------------------------------*/
const arr = [1, 2, 3];
/*--------------------------------------------------------*/
/* Instead of this:
----------------------------------------------------------*/
// const a = arr[0]
// const b = arr[1]
// const c = arr[2]
/*--------------------------------------------------------*/
/* Can do this:
----------------------------------------------------------*/
const [a, c] = arr;
// console.log(a, c);
/*--------------------------------------------------------*/
/* OR
----------------------------------------------------------*/
const [x, y] = ["Dima", "Anikin"]
console.log(x, y);
/*----------------------------------------------------------------------------------------------*/
/* Can also destructure an array when passed into a function
------------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------*/
/* Instead of this:
----------------------------------------------------------*/
// function sum(array){
// return array[0] + array[1] + array[2]
// }
/*--------------------------------------------------------*/
/* Can do this:
----------------------------------------------------------*/
const sum = ([a, b, c]) => a + b + c;
// console.log(sum(arr));