Answers for "Deep clone of multi-level object"

0

Deep clone of multi-level object

let obj = {x: 20, y: {z: 30}}; 

//long version  
const makeDeepClone = (obj) => {  
    let newObject = {};   
    Object.keys(obj).map(key => {  
        if(typeof obj[key] === 'object'){  
            newObject[key] = make DeepClone(obj[key]);   
        } else {  
            newObject[key] = obj[key];   
        }  
    });   
    return neObject;   
}  
const cloneObj = makeDeepClone(obj); 

//Shorthand  
const cloneObj = JSON.parse(JSON.stringify(obj));
Posted by: Guest on April-28-2021

Code answers related to "Deep clone of multi-level object"

Browse Popular Code Answers by Language