-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_objects_2.js
More file actions
120 lines (86 loc) · 3.13 KB
/
08_objects_2.js
File metadata and controls
120 lines (86 loc) · 3.13 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
// here declaring from a Constructor / Singelton
//const tinderUser = new Object()
const tinderUser = {}
tinderUser.id = "123abc",
tinderUser.name = "amCuc",
tinderUser.isLoggedIn = false
console.log(tinderUser); // we get an Object with a key and values
// Declaring Objects withIn and Objects
const regularUser = {
email: "some@gmail.com",
name: {
firstname : "Am",
lastName: " shak",
Address: {
Hnumber : "744A",
stnumber : 34
}
}
}
console.log(regularUser.Address.Hnumber);
//Optional Chaining Concept with Object
console.log(regularUser.name?.lastName); // example name dont exists then whats the prob,we use this "? " syntax did
//protetcion when getting response from API if this value exists then do otherwise have to put if and elase
// Combining or Mweging an Objects
const obj1 = {1: "a", 2: "b"}
const obj2 = {3: "c", 4: "d"}
//const obj3 = {obj1, obj2}; like this we get obj within Obj
// const obj3 = Object.assign({}, obj1, obj2);
// console.log(obj3); //we get proper array here {} --> target and merging Objects are source
// more than assign spread operator for merging is bein used
const Obj3 = {...obj1, ...obj2}
console.log(Obj3);
// When Getting Data From a Database, comes in the form of an Array of Objects
let dbUserValues = [
{
name: "sdff",
id: 1
},
{
name: "sdff",
id: 2
},
{
name: "sdff",
id: 3
},
{
name: "sdff",
id: 4
}
]
dbUserValues[1].name
console.log(Object.keys(tinderUser)); // return and array of Keys V imp concept further DB
// many a times we ask Obj that does it conain that property
console.log(tinderUser.hasOwnProperty('isLoggedIn')); // return True
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//DESTRUCTURING OBJECTS
//Destructuring of Objects and Arrays happens as well
// as we being doing destructing objects quite a Often as we will get and Object have to destructure to get the values
// Also See react hoew Values comes and how we destructure them
const course = {
courseName: "JS",
price: "999",
couseInstructor : "teacher"
}
// we gonna use these values by taking object and . as usual but when we want to make code Cleaner
// and dont write course.courseInstructor again and again, so we use a syntactical sugar
const {courseName : cn} = course // can also reassign a name at this point to make it smaller
console.log(courseName); // so we get its value
console.log(cn); // we get the same value
// DESTRUCTURING ARRAYS --> is rare not covered.
// API Concept: acts as a middleman between Client and backend
// Now values comes from backend in the form of JSON
// here in JSON the key and values are all in string format except number and boolean
// so from fetch method we call this url we get data and we need to convert it into json then we will
//convert that into object and can extract each values.
// {
// "name" : "am",
// "Id": 123
// "registered": true
// }
//Sometimes we also get API in Array Formatt mean data comes in the form of Array of Objects
[
{},
{},
]