Skip to content

Commit 73b7190

Browse files
Create removeCircularDependancy.js
1 parent e8096af commit 73b7190

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const obj = {
2+
a: 1,
3+
b: {
4+
c: 2,
5+
d: {
6+
e: 3,
7+
f: 4,
8+
g: 5,
9+
},
10+
},
11+
};
12+
obj.b.d.h = obj;
13+
14+
const removeCircularDependancy = (iterativeObj, compareObj) => {
15+
for (const key in iterativeObj) {
16+
const value = iterativeObj[key];
17+
if (typeof value === "object") {
18+
if (value === compareObj) {
19+
delete iterativeObj[key];
20+
} else {
21+
removeCircularDependancy(value, compareObj);
22+
}
23+
}
24+
}
25+
};
26+
27+
const output = removeCircularDependancy(obj, obj);
28+
console.log(obj);
29+
// output
30+
/*
31+
{
32+
a: 1,
33+
b: {
34+
c: 2,
35+
d: {
36+
e: 3,
37+
f: 4,
38+
g: 5,
39+
},
40+
},
41+
};
42+
*/

0 commit comments

Comments
 (0)