Handwritten function: recursively implement deep copy

function design

Function

Recursively implement deep copy of data

parameter

data being copied

return value

Copied new data

Ideas

1. You need to determine the data type first, which is mainly divided into three types: array type, object type, and other types.

2. For object types and array types, it is necessary to use recursion to dismantle them in depth until they become the remaining types.

3. For other types, they can be assigned as function return values.

accomplish

        //这里的深拷贝仅限于常规对象和常规数组
        function cloneDeep(value) {
            let initVal = null
            let valueType = toString.call(value)
            switch (valueType) {
                case '[object Object]':
                initVal = {}
                let arrs = Object.keys(value)
                if(arrs.length>0){
                    arrs.forEach((item)=>{
                  initVal[item] = cloneDeep(value[item])
                })
                return initVal
                }else{
                 return {}
                }
                    break
                case '[object Array]':
                let arr = []
                if(value.length>0){
                 value.forEach((item,index)=>{
                    arr[index] = cloneDeep(item)
                 })
                 return arr
                }else{
                    return []
                }
                    break
                default:
                    return value
            }
        }

test

    <script>
        //这里的深拷贝仅限于常规对象和常规数组
        function cloneDeep(value) {
            let initVal = null
            let valueType = toString.call(value)
            switch (valueType) {
                case '[object Object]':
                initVal = {}
                let arrs = Object.keys(value)
                if(arrs.length>0){
                    arrs.forEach((item)=>{
                  initVal[item] = cloneDeep(value[item])
                })
                return initVal
                }else{
                 return {}
                }
                    break
                case '[object Array]':
                let arr = []
                if(value.length>0){
                 value.forEach((item,index)=>{
                    arr[index] = cloneDeep(item)
                 })
                 return arr
                }else{
                    return []
                }
                    break
                default:
                    return value
            }
        }
       
        let family = [
            {
                name:'王惊涛',
                age:27,
                love:['吃饭','睡觉','写代码'],
                gender:true,
                work:{
                    name:'coder',
                    love:function(){
                        console.log('编码')
                    }
                }
            },
            {
                name:'马师',
                age:28,
                love:['吃饭','吃饭','还是吃饭'],
                gender:false,
                work:null,
                pi:undefined
            }
        ]
        let cloneShallow = family
        let cloneFamily = cloneDeep(family)
        console.log(cloneShallow === family,'浅拷贝')
        console.log(cloneFamily === family,'递归深拷贝')
        console.log(cloneFamily,'深拷贝后的对象')
    </script>

 

 

Guess you like

Origin blog.csdn.net/m0_54741495/article/details/132473165