-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_objects.js
More file actions
130 lines (104 loc) · 2.42 KB
/
06_objects.js
File metadata and controls
130 lines (104 loc) · 2.42 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
// ARRAYS ARE NOT IDEAL FOR STORING ALL TYPES OF DATA.
// We could use an array if we made sure we always follow the same pattern:
//index 0: total steps
//index 1: total floors
//etc.
const fitbitData = [ 308756, 1814, 211 ];
//We have no insight into WHAT we are storing at each index
//And we could mess things up super easily if we're not careful
const lucyFitbitData = [ 12344, 1814, 211 ];
// OBJECTS TO THE RESCUE!
// ===============
const palette = {
red: '#eb4d4b',
yellow: '#f9ca24',
blue: '#30336b'
};
//DOT NOTATION
palette.red; //'#eb4d4b'
//SQUARE BRACKET NOTATION
palette['yellow']; //'#f9ca24'
//With square brackets, we can also use dynamic key names:
let mysteryColor = 'blue';
palette[mysteryColor]; //'#30336b'
// ==============
const userReviews = {};
//Adding a new property:
userReviews['queenBee49'] = 4.0;
userReviews.mrSmith78 = 3.5;
//Updating existing properties
userReviews['queenBee49'] += 2;
userReviews.mrSmith78++;
// ==============
const student = {
firstName: 'David',
lastName: 'Jones',
strengths: [ 'Music', 'Art' ],
exams: {
midterm: 92,
final: 88
}
};
const avg = (student.exams.midterm + student.exams.final) / 2;
const shoppingCart = [
{
product: 'Jenga Classic',
price: 6.88,
quantity: 1
},
{
product: 'Echo Dot',
price: 29.99,
quantity: 3
},
{
product: 'Fire Stick',
price: 39.99,
quantity: 2
}
];
const game = {
player1: {
username: 'Blue',
playingAs: 'X'
},
player2: {
username: 'Muffins',
playingAs: 'O'
},
board: [ [ 'O', null, 'X' ], [ 'X', 'O', 'X' ], [ null, 'O', 'X' ] ]
};
// ============
const palette = {
red: '#eb4d4b',
yellow: '#f9ca24',
blue: '#30336b'
};
//Objects & Arrays are reference types
const palette2 = palette;
//If we change one value...
palette2.green = '#ebf876';
//Both variables reflect that change...
palette.green; //"#ebf876"
palette2.green; //"#ebf876"
// ============
let nums = [ 1, 2, 3 ];
let mystery = [ 1, 2, 3 ];
let moreNums = nums;
//They 'look' the same, but refer to different arrays
nums === mystery; // false
//These two arrays reference the exact same array, so we get true:
nums === moreNums; //true
const user = {
username: 'CherryGarcia8',
email: 'garcia@gmail.com',
notifications: [ 'message', 'alert' ]
};
//THIS WILL NOT WORK!
if (user.notifications === []) {
console.log('NO NEW NOTIFICATIONS!');
}
// THIS VERSION DOES WORK!
if (!user.notifications.length) {
console.log('NO NEW NOTIFICATIONS!');
}