JS数组对象:深拷贝方法
作者:
意随风起 | 来源:发表于
2022-07-02 20:43 被阅读0次function deepClone(obj) {
if (obj === null || !obj) return obj;
if (Object.prototype.toString.call(obj) === '[object Object]') {
let target = {};
const keys = Object.keys(obj);
keys.forEach((key) => {
if (obj[key] && typeof obj[key] === 'object') target[key] = deepClone(obj[key]);
else target[key] = obj[key];
});
return target;
} else if (Array.isArray(obj)) {
let arr: any[] = [];
obj.forEach((item, index) => {
if (item && typeof item === 'object') arr[index] = deepClone(item);
else arr[index] = item;
});
return arr;
}
}
本文标题:JS数组对象:深拷贝方法
本文链接:https://www.haomeiwen.com/subject/covkbrtx.html
网友评论