let obj= {
name: '王五',
arr: [2, 3],
colors: {
name: '赵四',
},
};
JSON.parse(JSON.stringfly(obj))
function deepClone(obj) {
if (typeof obj !== 'object' || obj == null) {
return obj;
}
let result;
if (obj instanceof Array) {
result = [];
} else {
result = {};
}
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = deepClone(obj[key]);
}
}
return result;
}
function deepclone (obj) {
const isobj = (obj) => typeof obj === 'object' && typeof obj !== 'null';
if (!isobj) return obj
const res = Array.isArray(obj) ? [...obj] : { ...obj };
Reflect.ownKeys(res).forEach((key) => {
res[key] = isobj(res[key]) ? deepclone(res[key]) : res[key];
});
return res;
};
注:有其他随时补充 有问题欢迎指出