-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path04_objects.js
66 lines (58 loc) · 2.13 KB
/
04_objects.js
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
// 4. Objects,
const tinderUser = new Object() // Singleton Object
// console.log(tinderUser); // Output: {}
const tinderUserTwo = {} // Non-singleton Object
// console.log(tinderUserTwo); // Output: {}
tinderUserTwo.id = "123abc"
tinderUserTwo.name = "sammy"
tinderUserTwo.isLoggedIn = false
// console.log(tinderUserTwo); // Output: { id: '123abc', name: 'sammy', isLoggedIn: false }
/*-------------------------------------------------------------------------------------------------
Objects into Objects, */
const regularUser = {
email: "someuser@gmail.com",
fullName: {
usersName: {
firstName: "Ayush",
lastName: "Yadav"
}
}
}
// console.log(regularUser.fullName.usersName.lastName); // Output: Yadav
//-------------------------------------------------------------------------------------------------
const obj1 = {1: "a", 2: "b"}
const obj2 = {3: "a", 4: "b"}
// Combining Objects,
// const obj3 = Object.assign({}, obj1, obj2) // {} Target | obj1, obj2 Source
// console.log(obj3); // Output: { '1': 'a', '2': 'b', '3': 'a', '4': 'b' }
// const obj3 = {...obj1, ...obj2} // used most of the time
// console.log(obj3); // Output: { '1': 'a', '2': 'b', '3': 'a', '4': 'b' }
/*-------------------------------------------------------------------------------------------------
Array of Objects, */
const user = [
{
id: 1,
email: "h@gmail.com"
},
{
id: 2,
email: "i@gmail.com"
},
{
id: 3,
email: "j@gmail.com"
},
{
id: 4,
email: "k@gmail.com"
}
]
// console.log(user[2].email); // Output: j@gmail.com
/*-------------------------------------------------------------------------------------------------
Some more impotant syntax, */
console.log(Object.keys(tinderUserTwo)); // Output: [ 'id', 'name', 'isLoggedIn' ]
console.log(Object.values(tinderUserTwo)); // Output: [ '123abc', 'sammy', false ]
console.log(Object.entries(tinderUserTwo)); /* Output: [ [ 'id', '123abc' ], [ 'name', 'sammy' ],
[ 'isLoggedIn', false ] ]
Checking Property exist or not, */
console.log(tinderUserTwo.hasOwnProperty('isLoggedIn')); // Output: true