-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestructuring-json.js
59 lines (40 loc) · 1.51 KB
/
destructuring-json.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
// 1.) Extract fields from JSON Object and store them in a variable
const person1 = {
userName: "Code Bobby",
skill: "Advanced Javascript"
}
const {userName, skill} = person1
console.log(userName);
console.log(skill);
console.log("===================================================");
// 2. Extract fields from JSON Object and store them in a new variable)
const person2 = {
normalUser: "Code BOB",
normalskill: "watching TV"
}
const {normalUser: loserName, normalskill: loserSkill} = person2
console.log(loserName);
console.log(loserSkill);
console.log("===================================================");
// 3.) Extract fields from JSON Object and store them in variable as well as create default value if field null
const player = {
playerName: "Roberto Codes"
}
const {
playerName: playerID,
playerSkill: playerSkill = "passing default Value Here"
} = player
console.log(playerID);
console.log(playerSkill);
console.log("===================================================");
// 4.) Extract fields from JSON Object and store them to new variable as well as create default value if field null
let workerName = {
employeeName: "Robi Rob"
}
let {
employeeName: workerID = "doesn't matter, can't reassign const", // new variable assignment workerID
workerSkill: workerTitle = "fire starter" // default new variable assignment workerTitle
} = workerName
console.log(workerID);
console.log(workerTitle);
console.log("Employee Name: " + workerID + " Employee Skill: " + workerTitle)