深拷贝
1.写法一
dateMap = JSON.parse(JSON.stringify(dateMap))
2.写法二
2.1写一个js文件
export {
superType,
deepCopy
};
const superType = (data) => {
const type = Object.prototype.toString.call(data).toLowerCase();
return type.replace(/^\[object\s(\w+)\]$/, (...rest) => {
return rest[1];
});
}
const deepCopy = (x) => {
if (superType(x) !== 'object') {
return '必须传入对象';
}
const target = Array.isArray(x) ? [] : {};
for (const k in x) {
if (x.hasOwnProperty(k)) {
if (superType(x[k]) === 'object') {
target[k] = deepCopy(x[k]);
} else {
target[k] = x[k];
}
}
}
return target;
}
2.2 引用,使用
import {deepCopy} from 'common/js/utils.js';
deepCopy(val)