新增 深克隆工具方便开发

This commit is contained in:
kangert 2021-06-18 11:11:20 +08:00
parent 134b63848a
commit 72ffa2d6fd

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;
}