-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path035-accessing-objects.js
More file actions
154 lines (122 loc) · 3.3 KB
/
035-accessing-objects.js
File metadata and controls
154 lines (122 loc) · 3.3 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 1: RECAP
// const person = {
// name: 'John',
// age: 25,
// city: 'New York'
// };
// 2: ACCESSING PROPERTIES - DOT NOTATION
// console.log(person.name); // John
// console.log(person.age); // 25
// console.log(person.city); // New York
// 3: ACCESSING PROPERTIES - BRACKET NOTATION
// console.log(person['name']); // John
// console.log(person['age']); // 25
// console.log(person['city']); // New York
// 4: WHEN TO USE BRACKET NOTATION
// const person = {
// 'first name': 'John',
// 'last-name': 'Doe',
// 'age@info': 25
// };
// Dot notation won't work here
// console.log(person.first name); // Syntax Error
// Bracket notation works perfectly
// console.log(person['first name']); // John
// console.log(person['last-name']); // Doe
// const anotherPerson = {
// name: 'John',
// age: 25,
// city: 'New York'
// };
// const propertyName = 'age';
// console.log(anotherPerson[propertyName]); // 25
// This is incredibly useful in dynamic situations
// const getUserInfo = (obj, key) => {
// return obj[key];
// };
// console.log(getUserInfo(anotherPerson, 'name')); // John
// console.log(getUserInfo(anotherPerson, 'city')); // New York
// 5: SETTING PROPERTIES
// const person = {
// name: 'John',
// age: 25,
// city: 'New York'
// };
// // Using dot notation
// person.age = 26;
// console.log(person.age); // 26
// // Using bracket notation
// person['city'] = 'Los Angeles';
// console.log(person.city); // Los Angeles
// const person = {
// name: 'John',
// age: 25
// };
// // Adding with dot notation
// person.city = 'New York';
// person.country = 'USA';
// console.log(person);
// { name: 'John', age: 25, city: 'New York', country: 'USA' }
// 6: DYNAMIC PROPERTY SETTING
// const person = {
// name: 'John',
// age: 25
// };
// const propertyName = 'profession';
// const propertyValue = 'Developer';
// person[propertyName] = propertyValue;
// console.log(person);
// { name: 'John', age: 25, profession: 'Developer' }
// 7: ACCESSING NON-EXISTENT PROPERTIES
// const person = {
// name: 'John',
// age: 25
// };
// console.log(person.city); // undefined
// console.log(person['country']); // undefined
// if (person.city !== undefined) {
// console.log('City exists:', person.city);
// } else {
// console.log('City property does not exist');
// }
// 8: PRACTICAL EXAMPLE
const product = {
id: 101,
name: 'Laptop',
price: 999,
brand: 'TechPro'
};
// Accessing properties
console.log('Product:', product.name);
console.log('Price: $' + product.price);
// Updating properties
product.price = 899;
console.log('New Price: $' + product.price);
// Adding new properties
product.inStock = true;
product.quantity = 50;
// Dynamic access
const displayProperty = (obj, key) => {
if (obj[key] !== undefined) {
console.log(`${key}: ${obj[key]}`);
} else {
console.log(`${key} not found`);
}
};
displayProperty(product, 'brand'); // brand: TechPro
displayProperty(product, 'color'); // color not found
// 9: NESTED OBJECT ACCESS
const user = {
name: 'Sarah',
age: 28,
address: {
street: '123 Main St',
city: 'Boston',
zipCode: '02101'
}
};
// Accessing nested properties
console.log(user.address.city); // Boston
console.log(user['address']['zipCode']); // 02101
// You can mix notation styles
console.log(user.address['street']); // 123 Main St