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