Simple manual deep-copy

function copyDeep(obj){
        if (typeof obj === 'object'&& obj!=null) {
            let target = Array.isArray(obj)?[]:{};
            for(let i in obj){
                if(Object.prototype.hasOwnProperty.call(obj,i)){
                    if(typeof obj === 'object'&& obj!=null){
                        target[i]=copyDeep(obj[i])
                    } else {
                        target[i]=obj[i]
                    }
                }
            }
            return target;
        } else {
            return obj;
        }
    }

 

Guess you like

Origin www.cnblogs.com/fewhj/p/11753562.html