添加深拷贝工具类

master
yujialong 3 years ago
parent 52cdc02a32
commit 282de476da
  1. 26
      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;
};
Loading…
Cancel
Save