From 570ab5a13c3e1fb7718af77efbb6d64bf1fa1a40 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Tue, 1 Aug 2023 14:17:30 +0800 Subject: [PATCH] fix/build-nobelium --- lib/utils.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index e13b192f..dc7cbeb6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -112,24 +112,32 @@ export function isIterable(obj) { return obj != null && typeof obj[Symbol.iterator] === 'function' } +/** + * 深拷贝对象 + * 根据源对象类型深度复制,支持object和array + * @param {*} obj + * @returns + */ export function deepClone(obj) { - const newObj = Array.isArray(obj) ? [] : {} - if (obj && typeof obj === 'object') { + if (Array.isArray(obj)) { + // If obj is an array, create a new array and deep clone each element + return obj.map(item => deepClone(item)) + } else if (obj && typeof obj === 'object') { + const newObj = {} for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { - if (obj[key] instanceof Date) { // 判断属性值是否为 Date 对象 - newObj[key] = new Date(obj[key].getTime()).toISOString() // 直接拷贝引用 - } else if (obj[key] && typeof obj[key] === 'object') { - newObj[key] = deepClone(obj[key]) + if (obj[key] instanceof Date) { + newObj[key] = new Date(obj[key].getTime()).toISOString() } else { - newObj[key] = obj[key] + newObj[key] = deepClone(obj[key]) } } } + return newObj + } else { + return obj } - return newObj } - /** * 延时 * @param {*} ms