|
15 | 15 | // console.log(person);
|
16 | 16 |
|
17 | 17 | // Dot notation
|
18 |
| -const person = { |
19 |
| - firstName: "Shubham", |
| 18 | +// const person = { |
| 19 | +// firstName: "Shubham", |
| 20 | +// }; |
| 21 | + |
| 22 | +// person.dog = { |
| 23 | +// name: "fluffy", |
| 24 | +// age: 2, |
| 25 | +// }; |
| 26 | + |
| 27 | +// person.age = 23; |
| 28 | +// console.log(person.dog.age); |
| 29 | + |
| 30 | +// Square bracket notation |
| 31 | +// const property = "age"; |
| 32 | + |
| 33 | +// console.log(person[property]); |
| 34 | + |
| 35 | +// Object.keys() creates an array containg the keys of an object. |
| 36 | + |
| 37 | +// Intialize an object |
| 38 | +const employee = { |
| 39 | + boss: "Shubham", |
| 40 | + secretary: "Samarth", |
| 41 | + sales: "John", |
| 42 | + accountant: "Oscar", |
20 | 43 | };
|
21 | 44 |
|
22 |
| -person.dog = { |
23 |
| - name: "fluffy", |
24 |
| - age: 2, |
| 45 | +// Let's say we want to see all the positions(keys) in the employee object |
| 46 | +const positions = Object.keys(employee); |
| 47 | +console.log(positions); |
| 48 | + |
| 49 | +// Object.values() creates an array containg the values of an object. |
| 50 | +const session = { |
| 51 | + id: 1, |
| 52 | + time: `26-July-2022`, |
| 53 | + device: "Mobile", |
| 54 | + browser: "Chrome", |
25 | 55 | };
|
26 | 56 |
|
27 |
| -person.age = 23; |
28 |
| -console.log(person.dog.age); |
| 57 | +const sessionInfo = Object.values(session); |
| 58 | +console.log(sessionInfo); |
29 | 59 |
|
30 |
| -// Square bracket notation |
31 |
| -const property = "age"; |
| 60 | +// Object.entries() creates an array containg the key/value of an object. |
| 61 | +const operatingSystem = { |
| 62 | + name: "Ubuntu", |
| 63 | + version: "20.04", |
| 64 | + license: "Open Source", |
| 65 | +}; |
| 66 | + |
| 67 | +const entries = Object.entries(operatingSystem); |
| 68 | + |
| 69 | +entries.forEach((entry) => { |
| 70 | + let key = entry[0]; |
| 71 | + let value = entry[1]; |
| 72 | + |
| 73 | + console.log(`${key}:${value}`); |
| 74 | +}); |
| 75 | + |
| 76 | +// Object.freeze() prevents modification to |
| 77 | +// properties and values of an object, |
| 78 | +// and prevents properties from being |
| 79 | +// added or removed from an object |
| 80 | + |
| 81 | +// Intialize an object |
| 82 | +// const user = { |
| 83 | +// username: "Shubham", |
| 84 | +// password: "12345", |
| 85 | +// }; |
| 86 | + |
| 87 | +// const admin = Object.freeze(user); |
| 88 | + |
| 89 | +// user.username = "Samarth"; // Trying to overwrite the object username |
| 90 | +// console.log(user.username); // Shubham (remains the same); |
| 91 | + |
| 92 | +// Object.seal() prevents new properties |
| 93 | +// from being added to an object, |
| 94 | +// but allows the modification of existing properties |
| 95 | +// Intialize an object |
| 96 | + |
| 97 | +const user = { |
| 98 | + username: "Shubham", |
| 99 | + password: "12345", |
| 100 | +}; |
| 101 | + |
| 102 | +const newUser = Object.seal(user); |
| 103 | + |
| 104 | +newUser.username = "Samarth"; // The username will be changed |
| 105 | +newUser.age = 23; // the age property will not be added because we applied Object.seal() |
32 | 106 |
|
33 |
| -console.log(person[property]); |
| 107 | +console.log(user); |
0 commit comments