forked from tools/tape-springboot-sysadmin
init
This commit is contained in:
26
src/iview/utils/deepClone.js
Normal file
26
src/iview/utils/deepClone.js
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
/**
|
||||
* 深度克隆对象
|
||||
*
|
||||
* @param {*} obj 要克隆的对象
|
||||
* @returns 克隆后的对象
|
||||
*/
|
||||
export function deepClone(obj) {
|
||||
const cache = new Map();
|
||||
function _deepClone(obj) {
|
||||
if (typeof obj !== 'object' || obj === null) {
|
||||
return obj;
|
||||
}
|
||||
if (cache.has(obj)) {
|
||||
return cache.get(obj);
|
||||
}
|
||||
const result = Array.isArray(obj) ? [] : {};
|
||||
cache.set(obj, result);
|
||||
for (const key in obj) {
|
||||
result[key] = _deepClone(obj[key]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
return _deepClone(obj);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user