-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
84 lines (55 loc) · 1.6 KB
/
main.js
File metadata and controls
84 lines (55 loc) · 1.6 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
//OBJECTS METHODS
//1. Object.keys(abj) -Returns an array of objects keys(property names)
const user = {
id: 1,
name: "David",
age: 40
}
console.log(Object.keys(user))//[ 'id', 'name', 'age' ]
//2. Object.values(abj)-Returns an array of an object values
const user2 = {
id: 2,
name: "Robert",
age: 50
}
console.log(Object.values(user2))//[ 2, 'Robert', 50 ]
//3.Object.entries(obj)-Returns an array of key-value pairs
const user3 = {
id: 3,
name: "Kim",
age: 20
}
console.log(Object.entries(user3))//[ [ 'id', 3 ], [ 'name', 'Kim' ], [ 'age', 20 ] ]
//4. Object.freeze(obj) -Prevents modification of an object
const user4 = {
id: 1,
name: "Ann",
age: 25
}
Object.freeze(user4)
user4.name = "Sarah"//wont change the object value
user4.age = 35//won't change object value
console.log(user4)//{ id: 1, name: 'Ann', age: 25 }
//5.Object.seal()-Prevents adding/removing properties, but you can still update existing values.
const user5 = {
name: "Alice",
age: 25
};
Object.seal(user);
user.age = 30; // works
user.city = "Nairobi"; // wont add
console.log(user); // { name: "Alice", age: 30 }
//6.Object.assign()-Copies values from one object to another.
const user6 = {
name: "Alice"
};
const details = {
age: 25,
city: "Nairobi"
};
const merged = Object.assign({}, user, details);
console.log(merged);// { name: "Alice", age: 25, city: "Nairobi" }
//7. Object.hasOwn() (or hasOwnProperty) -Checks if an object has a specific property.
const user7 = { name: "Alice", age: 25 };
console.log(Object.hasOwn(user7, "name")); // true
console.log(Object.hasOwn(user7, "city")); // false