-
Notifications
You must be signed in to change notification settings - Fork 129
/
Destructuring.js
executable file
·158 lines (130 loc) · 3.56 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
/*
* Destructuring
*
*/
/*
- "The destructuring assignment syntax is a
JavaScript expression that makes it possible
to extract data from arrays or objects into
distinct variables" - Mozilla Developer Network
- Destructuring syntax can be used on left-hand
side of assignments
*/
// Array destructuring
const myArray = [1, 2, 3, 4, 5];
const [x, y, z, a, b] = myArray;
const mySecondArray = ["Jane Doe", "John Doe", "Billy Bob"];
let name1, name2, name3, name4;
[name1, name2, name3, name4="I'm a default :D"] = mySecondArray;
// Swap two variables
let aa = 100;
let bb = 250;
let cc = 500;
// console.log(aa, bb, cc);
[bb, cc] = [cc, bb];
// Use with functions
function foodFilter(arr, term) {
return arr.filter((t) => t === term)
}
const [result] = foodFilter(["chicken", "fish", "tofu"], "tofu");
const [result2] = foodFilter(["pizza", "donut", "ice cream"], "pizza");
// Can ignore some values
const anotherArr = [10, 20, 50, 100, 1000, 30];
const [numA,,,,numD, ] = anotherArr;
// console.log(`First: ${numA}, second: ${numD}`);
// Can use rest pattern
const yetAnotherArr = ["dogs", "cats", "birds", "snakes", "hedgehogs"];
const [dogString, ...otherAnimals] = yetAnotherArr;
// Object destructuring
const myObj = {firstName: "Chris", lastName: "Jones", age: 16, height: "6ft"};
const { lastName, height, firstName } = myObj;
// console.log(lastName, height, firstName);
let myVar1, myVar2;
const myObj2 = {myVar1: "a", myVar2: "b"};
({ myVar1, myVar2 } = myObj2);
// Assigning new variable names
const myObj3 = { q: "cool!", w: "oh yeah!", e: "nice :D", r: "awesome!" };
const { q: cool, w, e: nice, r} = myObj3;
const myObj4 = { uuid: 134314309, userName: "es6rulez", loggedIn: new Date()};
const { uuid: userID, userName: name, loggedIn: lastLoginDate} = myObj4;
// console.log(lastLoginDate.toLocaleString());
// Defaults
const myObj5 = { a1: 10, b1: 20, c1: 30, d1: 40, e1: 100 };
const { a1, b1, c1, d1, e1 = 50} = myObj5;
function stateUser(obj) {
const { user = null, memberType } = obj;
if (!user) {
console.log("Error! No user name entered >:(");
return
} else {
console.log(`Username: ${user}, membership type: ${memberType}`);
}
}
const member1 = { user: "Steve", memberType: "premium"};
const member2 = { memberType: "free" };
function sayIfValid({prop, a111}) {
return {
prop,
a111,
constructed: true
}
}
const myObj6 = {
prop: "I am valid",
proop: "I am not valid",
a111: "I am also not valid",
}
const myConstructedObj = sayIfValid(myObj6);
const { constructed } = myConstructedObj;
// Nested objects
const myObj7 = {
title: "My address book",
entries: [
{
name: "Bob",
number: "555-5555",
address: "123 Fake St",
other: {
cell: "555-3333",
email: "[email protected]",
}
},
{
name: "Jill",
number: "555-1234",
address: "155 Fake St",
other: {
cell: null,
email: "[email protected]"
}
},
{
name: "Billy",
number: "555-8888",
address: "555 Fake St",
other: {
cell: "555-1233",
email: null
}
},
{
name: "Joan",
number: "123-4444",
address: "100 Fake Ave",
other: {
cell: "555-7779",
email: null
}
}
],
myPhone: "555-1111",
}
const { title, entries } = myObj7;
for (const {name: n, address: a, other:{cell, email}} of entries) {
console.log(`Name: ${n}, address: ${a}, cell: ${cell}, email: ${email}`);
}
const myObj8 = {
myProp1: "Ahhh!",
myProp2: [20, 50],
}
const { myProp1, myProp2: [ , yy]} = myObj8;