Skip to content

Commit 17c6c5d

Browse files
feat: add Object utility functions
1 parent 3dba09c commit 17c6c5d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// INFO: Objects Utility Functions
2+
3+
/*
4+
INFO: 1. Object.keys(obj)
5+
Returns an array of the object's own enumerable property names (keys).
6+
*/
7+
const user = {
8+
name: "rafay",
9+
age: 17,
10+
city: "Islamabad"
11+
};
12+
console.log(Object.keys(user));
13+
14+
/*
15+
INFO: 2. Object.values(obj)
16+
Returns an array of the object's own enumerable property values.
17+
*/
18+
console.log(Object.values(user));
19+
20+
/*
21+
INFO: 3. Object.entries(obj)
22+
Returns an array of the object's own enumberable property [key, value] pairs as arrays.
23+
*/
24+
console.log(Obbject.entries(user));
25+
26+
/*
27+
INFO: 4. Object.assign(target, ...sources)
28+
Copies enumerable properties from one or more source objects to a target object and returns the target object. Useful for cloning or merging objects.
29+
*/
30+
const target = {
31+
a: 1,
32+
};
33+
const source = {
34+
b: 2,
35+
c: 3
36+
};
37+
const returnedTarget = Object.assign(target, source);
38+
console.log(target);
39+
console.log(returnedTarget); // same as target
40+
41+
/*
42+
INFO: 5. Object.freeze(obj)
43+
Makes an object immutable - properties cannot be added, deleted, or changed.
44+
*/
45+
const config = {
46+
debug: true,
47+
};
48+
Object.freeze(config);
49+
50+
/*
51+
INFO: 6. Object.seal(obj)
52+
Prevents adding or deleting properties but allows modifying existing properties
53+
*/
54+
const profile = {
55+
name: "Rafay",
56+
};
57+
Object.seal(profile);
58+
profile.name = "Zain";
59+
console.log(profile);
60+
61+
/*
62+
INFO: Object.hasOwn(obj, "property")
63+
Checks if an object has a specific property
64+
*/
65+
const person = {
66+
name: "rafay",
67+
age: 20
68+
};
69+
console.log(Object.hasOwn(person, "age")); // true

0 commit comments

Comments
 (0)