对象deepcopy

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
  }

  

猜你喜欢

转载自www.cnblogs.com/yiyitong/p/9837993.html