-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathObjectExample.js
More file actions
210 lines (122 loc) · 3.63 KB
/
ObjectExample.js
File metadata and controls
210 lines (122 loc) · 3.63 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// In Javascript everything is object => it is top level in prototype hierachy
// Creating the object
// constructor method
// let employee = new Object();
// console.log(employee)
// let employee1 = {};
// employee.nam = "XYZ";
// employee1.lastName = "Hey Cool"
// console.log(employee , employee1)
// let employee2 = {
// name: "Xyx",
// lastName : "Hey LastName"
// }
// console.log(employee2)
// const employee3 = {};
// employee3.message= "Hey it is working ";
// console.log(employee3)
// Key
// let employee4 = {
// 'word1 word2': "Hey I am working"
// }
// console.log("4" , employee4)
// delete employee4["word1 word2"]
// console.log("4" , employee4)
// // shorthand Property
// const name1 = "Shorthand "
// const property= "Property chill out!!!!";
// // const properties = {
// // name1: name1,
// // property: property
// // }
// // console.log(properties) // {name1: 'Shorthand ', property: 'Property chill out!!!!'}
// const printMessage = ()=> {
// console.log("Hey")
// }
// const properties = {
// name1,
// property,
// printMessage
// }
// console.log(properties)
const employe = {
name: "TTTT",
mobile: "9915378881",
address: "BHOOT BUNGLOW",
salary: { a: 10000 , b: {amount: 1000}},
}
for(key in employe) {
console.log(key)
}
// if there is no key present in the object that will not throw the error it will simply give output as undefined
console.log(employe.company); // undefined
// console.log(employe.company.name) // .error
console.log(employe?.salary?.a?.amount?.fixedAmoutn)// . undefined
// console.log("salary" in employe) // true
// console.log('age' in employe)
// refernce variable
// comparison of the object variable
const com1 = {};
// const com2 = com1;
// console.log(com1==com2); // false => reference
// console.log(com1===com2); // false
// const com3 = {};
// console.log(com1==com3); // false
// console.log(com1===com3); // false
// // what const
const employ1 = {};
employ1.name = "Vishal";
employ1.name = "newton" // this will not throw the error
const data = employ1; // shallow copy
data.message = "Check and do it";
// deep copy
const deepObject = {}; // => it will point to the new ref not
for(key in data) {
console.log(key);
deepObject[key] = data[key]
}
deepObject.newMessage = "New Message are good";
console.log(employ1.newMessage); // undefined
// Object.assign
// uSE cASE OF THIS
// Cloning
// let empl4 = {name: "Newton" , college: "XYZ"};
// // let empl4Clone = Object.assign({}, empl4);
// let emplCopy = {};ct
// console.log('Hi' , emplCopy)
// Merging the object
const user = {
name :"Chandan",
nickName: "Chandu"
}
const user2 = {
college: "Chandu University"
}
Object.assign(user2 , user)
console.log(user2)
console.log(Object.keys(user))
// const data1 = Object.keys(user).map((key)=>{
// return {
// ... user[key],
// address: "Hey no address found"
// }
// })
const monthlExpenses = {
food: 10000,
dress: 20000,
fuel: 13000,
outing: 11000
}
// i have did not manage money
// Object.values(monthlExpenses) = > [10000 , 20000 , 13000, 11000].some
const check = Object.values(monthlExpenses).some((item)=> item>=20000);// true
const check1 = Object.values(monthlExpenses).every((item)=> item>=10000);// true
console.log(check)
console.log(check1)
// some and every related object
let someData = [100, 200, 300, 400 , 500]; // => greater than 480 than want to return true
let isTrue = someData.some((item , index) => {
console.log(item)
return item>480;
});
console.log(isTrue)