mirror of
https://gitee.com/log4j/pig-ui.git
synced 2024-12-23 05:40:20 +08:00
commit
57f666a69e
22
src/utils/deepClone.ts
Normal file
22
src/utils/deepClone.ts
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user