!6 新增 深克隆工具方便开发

Merge pull request !6 from kangert/master
This commit is contained in:
lyt-Top 2021-06-18 14:26:56 +08:00 committed by Gitee
commit 57f666a69e

22
src/utils/deepClone.ts Normal file
View File

@ -0,0 +1,22 @@
/**
*
* @param obj
* @returns
*/
export function deepClone(obj: any) {
let newObj: any
try {
//如果obj有push方法则 定义newObj为数组否则为对象。
newObj = obj.push ? [] : {}
} catch (error) {
newObj = {}
}
for (let attr in obj) {
if (typeof obj[attr] === 'object') {
newObj[attr] = deepClone(obj[attr]);
} else {
newObj[attr] = obj[attr];
}
}
return newObj;
}