Create a function that deep clones an object, including nested objects and arrays.
HardProblem Statement
Create a function that deep clones an object, including nested objects and arrays.
Sample Input
{
a: 1,
b: [ 1, 2, 3 ],
c: { d: 4, e: 5 } }Sample Output
{ a: 1, b: [ 1, 2, 3 ], c: { d: 4, e: 5 } }
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Deep clone an object, including nested structures
function deepClone(obj) {
// Check if the value is not an object or is null, return it
// If it is an array, map each element using deepClone
// For an object, iterate and deepClone each property
}
const original = { a: 1, b: [ 1, 2, 3 ], c: { d: 4, e: 5 } };
const cloned = deepClone(original);
console.log(cloned);
Input
{
a: 1,
b: [ 1, 2, 3 ],
c: { d: 4, e: 5 } }Expected Output
{ a: 1, b: [ 1, 2, 3 ], c: { d: 4, e: 5 } }