Files
springboot-shop-sysadmin/src/iview/utils/deepClone.js
iqudoo eb4e8f1a04 init
2026-06-05 17:22:32 +08:00

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