forked from tools/tape-springboot-sysadmin
27 lines
534 B
JavaScript
27 lines
534 B
JavaScript
|
|
/**
|
|
* 深度克隆对象
|
|
*
|
|
* @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);
|
|
}
|
|
|