Write a function `deepClone(obj)` that creates a deep copy of a plain object (no functions, no circular refs). Use `structuredClone` or JSON methods.
Problem Statement
Write a function `deepClone(obj)` that creates a deep copy of a plain object (no functions, no circular refs). Use `structuredClone` or JSON methods.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Shallow copy ({...obj}) only copies top-level properties.
š” Hint 2:
JSON.parse(JSON.stringify(obj)) creates a deep clone for plain objects.
š” Hint 3:
return JSON.parse(JSON.stringify(obj));
ā
Solution:
```javascript
function deepClone(obj) {
return JSON.parse(JSON.stringify(obj));
}
```