mirror of
https://gitee.com/log4j/pig-ui.git
synced 2024-12-23 05:40:20 +08:00
新增 深克隆工具方便开发
This commit is contained in:
parent
134b63848a
commit
72ffa2d6fd
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