-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path036-comparing-object.js
More file actions
176 lines (140 loc) · 4.65 KB
/
036-comparing-object.js
File metadata and controls
176 lines (140 loc) · 4.65 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Part 1: Removing Object Properties
// The Delete Operator
// const person = {
// name: 'Sarah',
// age: 28,
// city: 'New York',
// profession: 'Designer'
// };
// console.log(person);
// { name: 'Sarah', age: 28, city: 'New York', profession: 'Designer' }
// Now let's remove the 'city' property
// delete person.city;
// console.log(person);
// { name: 'Sarah', age: 28, profession: 'Designer' }
// const book = {
// title: 'JavaScript Basics',
// author: 'John Doe',
// pages: 350
// };
// Setting to undefined
// book.pages = undefined;
// console.log(book);
// { title: 'JavaScript Basics', author: 'John Doe', pages: undefined }
// console.log('pages' in book); // true - property still exists
// Actually deleting
// delete book.author;
// console.log(book);
// { title: 'JavaScript Basics', pages: undefined }
// console.log('author' in book); // false - property is gone
// The Delete Operator Returns a Boolean
// const car = {
// brand: 'Toyota',
// model: 'Camry'
// };
// const result = delete car.model;
// console.log(result); // true
// console.log(car); // { brand: 'Toyota' }
// Part 2: What Can't Be Deleted?
// 1. You cannot delete variables
// let myVariable = 10;
// delete myVariable; // This won't work
// console.log(myVariable); // 10 - still exists
// 2. You cannot delete properties inherited from prototypes
// const obj = {};
// delete obj.toString; // Won't delete because it's inherited
// console.log(obj.toString); // Still exists
// 3. You CAN delete properties from objects
// const user = { username: 'john123' };
// delete user.username; // This works
// console.log(user); // {}
// Part 3: Comparing Object Properties
// Comparing Object Properties Manually
// Primitive values
// const num1 = 5;
// const num2 = 5;
// console.log(num1 === num2); // true
// const str1 = 'hello';
// const str2 = 'hello';
// console.log(str1 === str2); // true
// // Objects - this is different!
// const obj1 = { name: 'Alice' };
// const obj2 = { name: 'Alice' };
// console.log(obj1 === obj2); // false
// const person1 = { name: 'Bob' };
// const person2 = person1; // person2 references the same object
// console.log(person1 === person2); // true - same reference
const laptop1 = {
brand: 'Dell',
ram: 16,
processor: 'Intel i7'
};
const laptop2 = {
brand: 'Dell',
ram: 16,
processor: 'Intel i7'
};
// Manual comparison
const areEqual =
laptop1.brand === laptop2.brand &&
laptop1.ram === laptop2.ram &&
laptop1.processor === laptop2.processor;
console.log(areEqual); // true
// Part 4: Comparing Objects with a Function
function compareObjects(obj1, obj2) {
// Get the keys of both objects
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
// If they have different number of properties, they're not equal
if (keys1.length !== keys2.length) {
return false;
}
// Check if all properties and values match
for (let key of keys1) {
if (obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
// Testing our function
const product1 = { id: 1, name: 'Phone', price: 599 };
const product2 = { id: 1, name: 'Phone', price: 599 };
const product3 = { id: 2, name: 'Laptop', price: 1299 };
console.log(compareObjects(product1, product2)); // true
console.log(compareObjects(product1, product3)); // false
// Part 5: Checking If a Property Exists
const smartphone = {
brand: 'Samsung',
model: 'Galaxy S21',
price: 799
};
// Method 1: Using 'in' operator
console.log('brand' in smartphone); // true
console.log('color' in smartphone); // false
// Method 2: Using hasOwnProperty()
console.log(smartphone.hasOwnProperty('model')); // true
console.log(smartphone.hasOwnProperty('storage')); // false
// Method 3: Checking if undefined
console.log(smartphone.price !== undefined); // true
console.log(smartphone.warranty !== undefined); // false
// Practical Example
const userProfile = {
username: 'coder123',
email: 'coder@example.com',
age: 25,
tempPassword: 'abc123',
isVerified: false
};
// Remove temporary data after user verification
if (userProfile.isVerified) {
delete userProfile.tempPassword;
delete userProfile.isVerified;
}
// Check if sensitive data exists before accessing
if ('tempPassword' in userProfile) {
console.log('Warning: Temporary password still present!');
} else {
console.log('Profile is clean.');
}
console.log(userProfile);