Skip to content

Commit b550806

Browse files
committed
Section 6: Adding and Updating Properties & Nested Arrays & Objects
1 parent b5d2dec commit b550806

2 files changed

Lines changed: 164 additions & 11 deletions

File tree

06-Objects-The-Core-of-Javascript/01/01.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ console.log(data2) */
3232
// ******************************
3333
// Accessing Object Properties ******************************
3434
// Example 2
35-
35+
/*
3636
const numbers = {
3737
100: 'one hundred',
3838
16: 'sixteen'
@@ -80,5 +80,114 @@ console.log(palette['123 hello hi']); // undefined
8080
console.log(palette.helloworld) // undefined
8181
console.log(palette[mysteryColor]) // '#f9ca24'
8282
console.log(palette['bl'+'ue']); // '#30336b'
83+
*/
84+
85+
// console.log('\n')
86+
// ******************************
87+
// Adding and Updating Properties ******************************
88+
// Example 4
89+
/*
90+
const userReviews = {
91+
}
92+
console.log(userReviews)
93+
// Using brackets
94+
userReviews['queenBee49'] = 4.0
95+
console.log(userReviews)
96+
console.log(userReviews['queenBee49'])
97+
98+
console.log('\n')
99+
// Using dot notation
100+
userReviews.mrSmith78 = 3.5
101+
console.log(userReviews['mrSmith78'])
102+
103+
console.log(userReviews.colt); // undefined
104+
userReviews.colt = '5'
105+
console.log(userReviews.colt)
106+
console.log(typeof userReviews.colt) // String
107+
userReviews['colt'] = 5
108+
console.log(userReviews.colt)
109+
console.log(typeof userReviews.colt); // Number
110+
console.log(userReviews)
111+
112+
console.log('\n')
113+
userReviews.queenBee49 += 2
114+
console.log(userReviews.queenBee49)
115+
116+
userReviews.mrSmith78++
117+
console.log(userReviews.mrSmith78) */
118+
119+
// console.log('\n')
120+
// ******************************
121+
// Nested Arrays & Objects ******************************
122+
// Example 5
123+
124+
const student = {
125+
firstName: 'Alberto',
126+
lastName: 'Guzman',
127+
strengths: ['Programmer', 'Futbol'],
128+
exams: {
129+
midterm: 92,
130+
final: 88,
131+
},
132+
}
133+
let avg = (student.exams.midterm + student.exams.final) / 2
134+
console.log('avg: ', avg)
135+
console.log(
136+
'Student strength:',
137+
student.strengths[0],
138+
',',
139+
student.strengths[1]
140+
)
141+
142+
console.log('\n')
143+
const tetrisGame = {
144+
player1: {
145+
username: 'ElfGodd',
146+
playingAs: 'X',
147+
},
148+
player2: {
149+
username: 'Josue',
150+
playingAs: 'O',
151+
},
152+
board: [
153+
['0', null, 'X'],
154+
['X', 'O', null],
155+
[null, 'O', 'X'],
156+
],
157+
}
158+
console.log(tetrisGame.player1.username)
159+
console.log(tetrisGame.player2.username)
160+
// Get length of array inside object
161+
// 2 ways of doing this
162+
console.log(tetrisGame.board.length);
163+
console.log(tetrisGame['board'].length)
164+
console.log('\n')
165+
for(i = 0; i < tetrisGame.board.length; i++) {
166+
for (j = 0; j < tetrisGame.board.length; j++) {
167+
console.log(tetrisGame.board[i][j])
168+
}
169+
}
170+
171+
console.log('\n')
172+
const shoppingCart = [
173+
{
174+
product: 'Jenga Classic',
175+
price: 6.88,
176+
quantity: 1,
177+
},
178+
{
179+
product: 'Echo Dot',
180+
price: 29.99,
181+
quantity: 3,
182+
},
183+
{
184+
product: 'Fire Stick',
185+
price: 39.99,
186+
quantity: 2,
187+
},
188+
]
83189

84-
// If i try to access
190+
console.log(shoppingCart)
191+
console.log(shoppingCart[0].product)
192+
console.log(shoppingCart[1].price)
193+
console.log(shoppingCart[2].quantity)

06-Objects-The-Core-of-Javascript/README.md

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ All keys are converted to strings, Except for Symbols
6969

7070
## 3. Accessing Object Properties
7171

72-
### ACCESSING DATA
72+
### ACCESSING DATA
7373

7474
```
7575
// This is converted
@@ -124,18 +124,62 @@ as a identifier like **let 76games = 'NBA team'**
124124

125125
## 4. Adding and Updating Properties
126126

127-
UPDATING & ADDING PROPERTIES
128-
// Updating properties: fitBitData.workoutsThisWeek = '6 of 7'; fitBitData.totalMiles += 7.5;
127+
It's similar to viewing a property or accessing the data. We have the dot notation or the square brackets. The only difference is we use an equal sign to assign a value either to update that value or to give it an initial value.
129128

130-
// Adding a new property fitBitData.heartStillBeating = true;
129+
```
130+
// Updating properties:
131+
fitAppData.workoutsThisWeek = '6 of 7';
132+
fitAppData.totalMiles += 7.5;
133+
134+
// Adding a new property
135+
fitAppData.heartStillBeating = true;
136+
```
131137

132-
ARRAYS + OBJECTS
133-
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 } ]
138+
## 5. Nested Arrays & Objects
134139

135-
const student = { firstName: 'David', lastName: 'Jones', strengths: ['Music', 'Art'], exams: { midterm: 92, final: 88 } }
140+
### ARRAYS + OBJECTS
141+
```
142+
const shoppingCart = [
143+
{
144+
product: 'Jenga Classic',
145+
price: 6.88,
146+
quantity: 1
147+
},
148+
{
149+
product: 'Echo Dot',
150+
price: 29.99,
151+
quantity: 3
152+
},
153+
{
154+
product: 'Fire Stick',
155+
price: 39.99,
156+
quantity: 2
157+
}
158+
]
159+
```
136160

137-
Nested Arrays & Objects
138-
const game = { player1: { username: 'Blue', playingAs: 'X' }, player2: { username: 'Muffins', playingAs: '0' }, board : [ ['0', null, 'X'], ['X', '0', 'X'], [null, '0', 'X'] ] };
161+
```
162+
const student = {
163+
firstName: 'Alberto',
164+
lastName: 'Guzman',
165+
strengths: ['Programmer', 'Futbol'],
166+
exams: {
167+
midterm: 92,
168+
final: 88
169+
}
170+
}
171+
```
172+
173+
```
174+
const game = {
175+
player1: { username: 'Blue', playingAs: 'X' }
176+
player2: { username: 'Muffins', playingAs: '0' }
177+
board : [
178+
['0', null, 'X'],
179+
['X', '0', 'X'],
180+
[null, '0', 'X']
181+
]};
182+
```
139183

140184
Objects and Reference Types
141185
Objects and Arrays are Reference types

0 commit comments

Comments
 (0)