We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e8096af commit 73b7190Copy full SHA for 73b7190
Interview Question/removeCircularDependancy.js
@@ -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
33
34
35
36
37
38
39
40
41
42
+*/
0 commit comments