Skip to content

Commit 0823058

Browse files
committed
02 Basics / 04 Objects JS
Learned about constructors method, objects into objects, combining objects, array of objects and some important syntax to access objects.
1 parent d8384df commit 0823058

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

02_Basics/04_objects.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// 4. Objects,
2+
3+
const tinderUser = new Object() // Singleton Object
4+
// console.log(tinderUser); // Output: {}
5+
6+
const tinderUserTwo = {} // Non-singleton Object
7+
// console.log(tinderUserTwo); // Output: {}
8+
9+
tinderUserTwo.id = "123abc"
10+
tinderUserTwo.name = "sammy"
11+
tinderUserTwo.isLoggedIn = false
12+
// console.log(tinderUserTwo); // Output: { id: '123abc', name: 'sammy', isLoggedIn: false }
13+
14+
/*-------------------------------------------------------------------------------------------------
15+
Objects into Objects, */
16+
const regularUser = {
17+
email: "someuser@gmail.com",
18+
fullName: {
19+
usersName: {
20+
firstName: "Ayush",
21+
lastName: "Yadav"
22+
}
23+
}
24+
}
25+
// console.log(regularUser.fullName.usersName.lastName); // Output: Yadav
26+
27+
//-------------------------------------------------------------------------------------------------
28+
const obj1 = {1: "a", 2: "b"}
29+
const obj2 = {3: "a", 4: "b"}
30+
// Combining Objects,
31+
// const obj3 = Object.assign({}, obj1, obj2) // {} Target | obj1, obj2 Source
32+
// console.log(obj3); // Output: { '1': 'a', '2': 'b', '3': 'a', '4': 'b' }
33+
// const obj3 = {...obj1, ...obj2} // used most of the time
34+
// console.log(obj3); // Output: { '1': 'a', '2': 'b', '3': 'a', '4': 'b' }
35+
36+
/*-------------------------------------------------------------------------------------------------
37+
Array of Objects, */
38+
const user = [
39+
{
40+
id: 1,
41+
email: "h@gmail.com"
42+
},
43+
{
44+
id: 2,
45+
email: "i@gmail.com"
46+
},
47+
{
48+
id: 3,
49+
email: "j@gmail.com"
50+
},
51+
{
52+
id: 4,
53+
email: "k@gmail.com"
54+
}
55+
]
56+
// console.log(user[2].email); // Output: j@gmail.com
57+
58+
/*-------------------------------------------------------------------------------------------------
59+
Some more impotant syntax, */
60+
console.log(Object.keys(tinderUserTwo)); // Output: [ 'id', 'name', 'isLoggedIn' ]
61+
console.log(Object.values(tinderUserTwo)); // Output: [ '123abc', 'sammy', false ]
62+
console.log(Object.entries(tinderUserTwo)); /* Output: [ [ 'id', '123abc' ], [ 'name', 'sammy' ],
63+
[ 'isLoggedIn', false ] ]
64+
65+
Checking Property exist or not, */
66+
console.log(tinderUserTwo.hasOwnProperty('isLoggedIn')); // Output: true

0 commit comments

Comments
 (0)