-
Notifications
You must be signed in to change notification settings - Fork 0
/
Destructuring.js
134 lines (111 loc) · 2.87 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
// ==================
// Destructuring
// ==================
//Clean syntax to unpack values from arrays/objects into distinct variables
//----------------------
// ARRAY DESTRUCTURING
//----------------------
const scores = [929321, 899341, 888336, 772739, 543671, 243567, 111934];
// const highScore = scores[0]; traditional way
// const secondHighScore = scores[1];
const [gold, silver, bronze, ...everyoneElse] = scores; //destructuring
//gold contains the highest score
//everyoneElse contains the array of losers
//----------------------
// OBJECT DESTRUCTURING
//----------------------
const user = {
email: '[email protected]',
password: 'sCoTt1948sMiTh',
firstName: 'Harvey',
lastName: 'Milk',
born: 1930,
died: 1978,
bio: 'Harvey Bernard Milk was an American politician and the first openly gay elected official in the history of California, where he was elected to the San Francisco Board of Supervisors',
city: 'San Francisco',
state: 'California'
}
const user2 = {
email: '[email protected]',
firstName: 'Stacy',
lastName: 'Gonzalez',
born: 1987,
city: 'Tulsa',
state: 'Oklahoma'
}
// const firstName = user.firstName; //tradtional way
// const lastName = user.lastName;
// const email = user.email;
const { email, firstName, lastName, city, bio } = user; //cool way
// const { born: birthYear, died: deathYear = 'N/A' } = user; //property:newVarname, born.user access now
// const { city, state, died = 'N/A' } = user2;
//----------------------
// PARAMETER DESTRUCTURING
//----------------------
// function fullName(user) {
// return `${user.firstName} ${user.lastName}`
// }
//cool way 1
// function fullName(user) {
// const { firstName, lastName } = user;
// return `${firstName} ${lastName}`
// }
//cool way 2
function fullName({ firstName, lastName }) {
return `${firstName} ${lastName}`
}
const movies = [
{
title: 'Amadeus',
score: 99,
year: 1984
},
{
title: 'Sharknado',
score: 35,
year: 2013
},
{
title: '13 Going On 30',
score: 70,
year: 2004
},
{
title: 'Stand By Me',
score: 85,
year: 1986
},
{
title: 'Waterworld',
score: 62,
year: 1995
},
{
title: 'Jingle All The Way',
score: 71,
year: 1996
},
{
title: 'Parasite',
score: 95,
year: 2019
},
{
title: 'Notting Hill',
score: 77,
year: 1999
},
{
title: 'Alien',
score: 90,
year: 1979
}
]
// movies.filter((movie) => movie.score >= 90)
// movies.filter(({ score }) => score >= 90)
// movies.map(movie => {
// return `${movie.title} (${movie.year}) is rated ${movie.score}`
// })
movies.map(({ title, score, year }) => {
return `${title} (${year}) is rated ${score}`
})