Deep Copy
MediumP1Coding
Implement deepCopy(value).
Return a deep copy of value. It must handle:
- primitives (returned as they are)
- plain objects and arrays, at any depth
Date,Map,SetandRegExp- circular references: a copy of a cyclic structure is cyclic in the same way, and shared references stay shared (if two properties point to the same object, the copies point to one copy)
Functions can be returned as they are. Do not use structuredClone or JSON.parse(JSON.stringify(...)).
Example 1
- Input
- const a = { x: [1, { y: 2 }] }; const b = deepCopy(a);
- Output
- b equals a, but b !== a and b.x[1] !== a.x[1]
Constraints
- Nesting depth up to 1000
- Only own enumerable string keys need to be copied; prototypes of plain objects can be Object.prototype
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.