Let's keep it simple. Logic is quite clear: if the input is not object or array, return it. Otherwise, call the function recursively. To avoid circular, just create a history to save each of value it sees at every step. Here is my suggestion (not tested):
I am a believer of the Root Cause Analysis. Ask "How, What, Why" until you can dissect it with no further to go. Well, I am a frontend developer ready for new challenges!
Hi Dong, this is a charming solution. It worked beautifully for all the nested objects, arrays, and primitive.
However, I tested a couple of scenarios for the cyclic object. And I found a behavior which is quite interesting. I hope to figure this out together-
//original objectletoriginal={a:1,b:[1,2],c:[1,[2,3]],d:{d1:1,d2:[1,2],d3:{}}};original.d.d3=original.d;letdeep=clone(original);//after which I did the following modificationsdeep.d.d1='saloni';deep.d.d3.d1='yadav';
Observations:
The first hierarchy of circular object is deep cloned
From the second hierarchy onwards, the cloning is shallow, both the current object and the original object.
Required:
The circular dependency should be shallow within the same object, so even when I change the first parent in the deep obj, all the corresponding children should point to the same obj.
This should however, not effect the original object at all.
This is scenario is obviously a very complex version of our use case. And I think the solution lies in how we use the Set or the Map. However, I must mention that your solution works amazingly well for a non-circular n-depth object. So kudos and thank you for that!! 🤩
my pleasure! Thank you for your compliments.
Shallowing is a big issue here because we need a deepClone method. Please try to fix it :)
In another reply you mentioned to lodash. I've taken a look on their solution: github.com/lodash/lodash/blob/mast...
The problem seems to be more complicated than we think, so they need a lot of code to deal with.
I am a believer of the Root Cause Analysis. Ask "How, What, Why" until you can dissect it with no further to go. Well, I am a frontend developer ready for new challenges!
Hey, thanks for sharing the source code. It is pretty huge with a lot of cases for various scenarios. Indeed looks like its way more twisted than a recursive call. Pretty interesting all the same!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Let's keep it simple. Logic is quite clear: if the input is not object or array, return it. Otherwise, call the function recursively. To avoid circular, just create a history to save each of value it sees at every step. Here is my suggestion (not tested):
Hi Dong, this is a charming solution. It worked beautifully for all the nested objects, arrays, and primitive.
However, I tested a couple of scenarios for the cyclic object. And I found a behavior which is quite interesting. I hope to figure this out together-
Observations:
Required:
deep
obj, all the corresponding children should point to the same obj.original
object at all.This is scenario is obviously a very complex version of our use case. And I think the solution lies in how we use the Set or the Map. However, I must mention that your solution works amazingly well for a non-circular n-depth object. So kudos and thank you for that!! 🤩
my pleasure! Thank you for your compliments.
Shallowing is a big issue here because we need a deepClone method. Please try to fix it :)
In another reply you mentioned to lodash. I've taken a look on their solution:
github.com/lodash/lodash/blob/mast...
The problem seems to be more complicated than we think, so they need a lot of code to deal with.
Hey, thanks for sharing the source code. It is pretty huge with a lot of cases for various scenarios. Indeed looks like its way more twisted than a recursive call. Pretty interesting all the same!