-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjRecursion.js
More file actions
89 lines (71 loc) · 2.17 KB
/
Copy pathobjRecursion.js
File metadata and controls
89 lines (71 loc) · 2.17 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
export let user = {
name: "Sindhu",
address: {
personal: {
street: "134 N Butler Street",
Apt: "2",
zip: "53703",
city: "Madison",
state: "WI"
},
work: {
street: "400 Jefferson Park",
zip: "07981",
city: "Whippany",
state: "NJ"
}
},
phone: 6085909893,
age: 27
}
/******************************************************************************************/
/**
*
* @param {*} obj --> object to be parsed
* @param {*} parentKey --> key of the parent object
*/
let returnObj = {};
export let combineFields = (obj, parentKey) => {
for(let key in obj) {
if(typeof(obj[key]) === "object") {
combineFields(obj[key], `${String(parentKey)}_${key}`)
}
else {
returnObj[`${String(parentKey)}_${key}`] = obj[key]
}
}
return returnObj;
}
/******************************************************************************************/
const clonedObject = {};
export function deepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj; // Base case: return non-object values or null as is
}
if (Array.isArray(obj)) {
// Handle arrays separately
return obj.map(deepClone); // Recursively apply deepClone to each array element
}
// Handle regular objects
for (const key in obj) {
if (obj.hasOwnProperty(key)) { // hasOwnProperty will only check the keys of the object, not the ones in its prototype unlike includes() function
clonedObject[key] = deepClone(obj[key]); // Recursively apply deepClone to each object property
}
}
return clonedObject;
}
/******************************************************************************************/
export let objDeepCloneDuplicate = obj => {
if (typeof obj !== 'object' || obj === null) {
return obj
}
if (Array.isArray(obj)) {
obj.map(objDeepCloneDuplicate)
}
for(let key in obj) {
if (obj.hasOwnProperty(key)) {
clonedObject[key] = objDeepCloneDuplicate(obj[key])
}
}
return clonedObject;
}