首页 > 其他分享 >js递归实现深拷贝

js递归实现深拷贝

时间:2024-04-23 11:48:26浏览次数:12  
标签:递归 res clone Object js type 拷贝 else data

function getType(data) {
  return Object.prototype.toString.call(data).replace(']', '').substring(8)
}

function _clone(data, res) {
  const type = getType(data)
  if (type === 'Object') {
    Object.keys(data).map(i => {
      if (res[i] === 'Object') {
        res[i] = _clone(data[i], {})
      } else if (res[i] === 'Array') {
        res[i] = _clone(data[i], [])
      } else {
        res[i] = data[i]
      }
    })
  } else if (type === 'Array') {
    data.map(i => {
      const type2 = getType(i)
      if (type2 === 'Object') {
        res.push(_clone(i, {}))
      } else if (type2 === 'Array') {
        res.push(_clone(i, []))
      } else {
        res.push(i)
      }
    })
  } else {
    res = data
  }
  return res
}

function clone(data) {
  const type = getType(data)
  if (type === 'Array') {
    return _clone(data, []) 
  } else if (type === 'Object') {
    return _clone(data, {})
  } else {
    return data
  }
}

const test = {
  name: '张三', age: 18, login: true, habbies: ['打球', {
    a: 'javascript', b: ['vue', {
      [1]: 'react',
      [2]: 'angule'
    }]
  }]
}
const result1 = clone(test)
result1.name = '李四'
result1.habbies = '没有兴趣爱好'

console.log(test, result1)

 

标签:递归,res,clone,Object,js,type,拷贝,else,data
From: https://www.cnblogs.com/fmg0224/p/18152498

相关文章

  • js AddDays ,AddYears
    //日期类型变量格式化,默认格式"xxxx-xx-xx"Date.prototype.Format=function(fmt){fmt=fmt||"yyyy-MM-dd";varo={"M+":this.getMonth()+1,//月份"d+":this.getDate(),//日"h+":......
  • three.js使用Instanced Draw+Frustum Cull+LOD来渲染大场景(开源)
    大家好,本文使用three.js实现了渲染大场景,在移动端也有较好的性能,并给出了代码,分析了关键点,感谢大家~关键词:three.js、InstancedDraw、大场景、LOD、FrustumCull、优化、Web3D、WebGL、开源代码:Github我正在承接Web3D数字孪生项目,具体介绍可看承接各种Web3D业务加QQ群交流:106......
  • fastjson导致的程序崩溃:A fatal error has been detected by the Java Runtime Enviro
    ##AfatalerrorhasbeendetectedbytheJavaRuntimeEnvironment:##EXCEPTION_ACCESS_VIOLATION(0xc0000005)atpc=0x000001da4d3ab6b3,pid=15996,tid=0x0000000000006478##JREversion:Java(TM)SERuntimeEnvironment(8.0_361)(build1.8.0_361-b09)......
  • gojs2.1去水印
    go.jsgo-debug.jsgo-module.jsES6模块go-debug-module.js这四个文件,具体看你使用哪一个搜索String.fromCharCode找到下图位置,这是我的格式化之后的样子 然后在 returnf 之前添加代码if(f.indexOf('GoJS2.1evaluation')>-1||f.indexOf('(c)1998-......
  • vis.js摄像机位置
    代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><style>body{font:10ptarial;}td{font:10ptarial;}</style><s......
  • vis.js绘制3d图形
    案例1代码案例<!DOCTYPEhtml><html><head><title>Graph3Ddemo</title><scripttype="text/javascript"src="https://unpkg.com/vis-graph3d@latest/dist/vis-graph3d.min.js"><......
  • vis.js分组多图折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js着色
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js滚动折线图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......
  • vis.js散点图
    代码案例<!doctypehtml><html><head><title>Timeline</title><scripttype="text/javascript"src="https://unpkg.com/vis-timeline@latest/standalone/umd/vis-timeline-graph2d.min.js"></script>......