首页 > 其他 > 详细

对象deepcopy

时间:2018-10-23 19:04:20      阅读:160      评论:0      收藏:0      [点我收藏+]
export const deepCopy = (dst, ori) => {
    let keys = Object.keys(ori)
    keys.forEach((key) => {
      if (typeof ori[key] === ‘object‘) {
        if (Array.isArray(ori[key])) {
          if (!Array.isArray(dst[key])) {
            dst[key] = []
            ori[key].forEach((el) => {
              dst[key].push(deepCopy({}, el))
            })
          } else {
            ori[key].forEach((el, index) => {
              deepCopy(dst[key][index], el)
            })
          }
        }
        else if (ori[key] !== null) {
          dst[key] = {}
          deepCopy(dst[key], ori[key])
        } else {
          dst[key] = null
        }
      } else if (typeof ori[key] === ‘function‘) {
        // do nothing
      } else {
        if (typeof dst === ‘undefined‘) {
          console.info(dst, ori)
        }
        dst[key] = ori[key]
      }
    })
    return dst
  }

  

对象deepcopy

原文:https://www.cnblogs.com/yiyitong/p/9837993.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!