[常用函数]深拷贝

判断类型见:https://www.cnblogs.com/zhoulixue/p/17187385.html

// 深拷贝
export const deepclone = (source: any) => {
  if (getType(source) === 'Object') {
    return Object.keys(source).reduce((pre, curr) => {
      const res = pre;
      res[curr] = deepclone(source[curr]);
      return res;
    }, {});
  }
  if (getType(source) === 'Array') {
    return source.map((x: any) => deepclone(x));
  }
  if (getType(source) === 'Date') {
    return new Date(source);
  }
  if (getType(source) === 'RegExp') {
    return new RegExp(source.source, source.flags);
  }
  return source;
};

 

posted @ 2023-03-07 11:15  zhoulixue  阅读(15)  评论(0编辑  收藏  举报