标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:What is the most efficient way to deep clone an object in JavaScript?70 answersI am trying to deep copy array of nested objects in javascript. My array look like this var arr = [{name:"adam",age:"21"},{name:"freddie",age:"35",children:[{name:"mercury",age:"25"}]},{name:"jim",age:"35",children:[{name:"morrison",age:"25",children:[{name:"some", age:"40"}]}]}];I want to make a deep copy of every object inside the array that is i want to create a exact copy of arr into new array which should not have object reference. Depth of array is also unknown that is children array can be upto any level.I have gone through this linkCopying of an array of objects to another Array without object reference in javascript(Deep copy) but that did not help me. I googled and found some solutions in jQuery but that did not help me as i dont have knowledge of jQuery.I also tried implementing it with recursion but that's not working too http://ideone.com/kJi5X3I want to do it in javascript only without using jQuery or anything. I am new to JavaScript so i may have missed if there is any library or simple method to do this.Please help me to solve this problem. Thanks in advance.


You linked to a question that this could have been closed as a duplicate of and said it didn't help - can you clarify why it didn't help, so we understand why this isn't a duplicate of that one?

2019年04月25日18分50秒

Also note that jQuery is just a javascript library, if there was a jQuery solution, are you ruling out that library while another one would be acceptable?

2019年04月25日18分50秒

James Thorpe - No i am wondering if there is any way to do it with recursion or any other logic i dont have knowledge of jQuery and i want to do this ASAP.

2019年04月25日18分50秒

James That link did not help me because it was not working for nested objects.

2019年04月25日18分50秒

Regarding that potential duplicate - please read beyond the top voted accepted answer - indeed note that that answer itself points you to one of the others.

2019年04月25日18分50秒

Crowder that worked flawlessly, Thank you very much :)

2019年04月25日18分50秒