From 282de476dac639837374cc151cf322ad04d3385d Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Fri, 3 Sep 2021 18:07:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B7=B1=E6=8B=B7=E8=B4=9D?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/deepCopy.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/utils/deepCopy.js diff --git a/src/utils/deepCopy.js b/src/utils/deepCopy.js new file mode 100644 index 0000000..ca4a515 --- /dev/null +++ b/src/utils/deepCopy.js @@ -0,0 +1,26 @@ +export const deepCopy = function(obj) { + if (obj == null) { + return null; + } + if (typeof obj !== "object") return obj; + let result; + if (Array.isArray(obj)) { + result = []; + obj.forEach(item => { + result.push( + typeof item === "object" && !(item instanceof Date) + ? deepCopy(item) + : item + ); + }); + } else { + result = {}; + Object.keys(obj).forEach(key => { + result[key] = + typeof obj[key] === "object" && !(obj[key] instanceof Date) + ? deepCopy(obj[key]) + : obj[key]; + }); + } + return result; +};