From 6ff7a95cdca915198c10592422d42c85ee4b3790 Mon Sep 17 00:00:00 2001 From: Dong Xia Date: Thu, 28 Nov 2024 14:21:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E8=B1=A1=E6=B7=B1=E6=8B=B7=E8=B4=9D?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E5=87=BD=E6=95=B0=E4=BB=A5=E5=8F=8A=E5=B8=B8?= =?UTF-8?q?=E7=94=A8=E5=AF=B9=E8=B1=A1=E5=A4=84=E7=90=86=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/node/list/index.vue | 5 ++-- src/utils/obj.js | 48 +++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/utils/obj.js diff --git a/src/pages/node/list/index.vue b/src/pages/node/list/index.vue index 40aad6f..0936915 100644 --- a/src/pages/node/list/index.vue +++ b/src/pages/node/list/index.vue @@ -179,6 +179,7 @@ import NodeLayout from './node-layout' import NodeOperation from './components/node_operation.vue' import { isIpAndPort } from '@/utils/validate' import { mapState } from 'vuex' +import { deepClone } from '@/utils/obj' export default { components: { NodeLayout, @@ -338,7 +339,7 @@ export default { item['status'] = 1 this.list = [...this.list] // 获取到节点调用一次编辑接口 - const query = JSON.parse(JSON.stringify(item)) + const query = deepClone(item) query.type = '' editNode(query) } else { @@ -438,7 +439,7 @@ export default { cancelText: '取消', onOk: () => { // 注册 - let query = JSON.parse(JSON.stringify(record)) + let query = deepClone(record) query.checkStatus = true query.type = 'edit' editNode(query).then((res) => { diff --git a/src/utils/obj.js b/src/utils/obj.js new file mode 100644 index 0000000..765d9b4 --- /dev/null +++ b/src/utils/obj.js @@ -0,0 +1,48 @@ +// 深拷贝简单的对象或数组 +export function deepClone(obj) { + return JSON.parse(JSON.stringify(obj)); + } + +/** + * 深度克隆对象(针对复杂的对象或数组) + * @param {any} obj - 需要克隆的对象 + * @returns {any} 克隆后的对象 + */ +function isObject(obj) { + return (typeof obj === 'object' && obj !== null) || typeof obj === 'function'; +} + +export function deepCloneV2(obj, seen = new WeakMap()) { + if (!isObject(obj)) { + return obj; + } + + if (seen.has(obj)) { + return seen.get(obj); + } + + let cloneObj; + + if (obj instanceof Date) { + cloneObj = new Date(obj); + } else if (obj instanceof RegExp) { + cloneObj = new RegExp(obj); + } else { + cloneObj = Array.isArray(obj) ? [] : {}; + } + + seen.set(obj, cloneObj); + + for (const key in obj) { + if (obj.hasOwnProperty(key)) { + cloneObj[key] = deepClone(obj[key], seen); + } + } + + return cloneObj; +} + +// 数组去重 +export function unique(arr) { + return [...new Set(arr)]; + } \ No newline at end of file -- Gitee