0

I have a complicated structure shown bellow,

enter image description here

This structure is nested, but I need to deep-copy it. My copy function is

export function copy(o) {
   if(typeof o==='object' || Array.isArray(o)) return 
   JSON.parse(JSON.stringify(o));
   return o;
}

When I use this function to copy the structure, the error shows,

Uncaught TypeError: Converting circular structure to JSON
at JSON.stringify (<anonymous>)
at copy (util.jsx:50)

Any good ideas about how to copy nested structures? Any suggestions are welcome.

Yu Fang
  • 520
  • 5
  • 17
  • 1
    You have functions and symbols there, you can't use JSON.stringify/parse – CertainPerformance Dec 19 '18 at 02:50
  • You're correct - in this case,JSON.stringify() won't work if the OP wants to retain the functions and/or symbols. But in other cases, it can actually be the most efficient (and simplest!) way to "deep copy". Q: Of the many alternatives in the "duplicate" you cited, which one would you recommend to the OP? – paulsm4 Dec 19 '18 at 02:55
  • 1
    You shouldn't mark as a duplicate when the answer gives a jQuery solution, time for an update. – Adrian Brand Dec 19 '18 at 03:02

0 Answers0