- 首页 > it技术 > >
function deepCopy(data) {
let typeOf = function(data) {
const toString = Object.prototype.toString;
const map = {
'[object Array]': 'array',
'[object Boolean]':'boolean',
'[object Object]':'object',
'[object Number]':'number',
'[object Function]': 'function',
'[object String]': 'string',
'[object RegExp]': 'regExp',
'[object Data]': 'date',
'[object Undefined]': 'undefined',
'[object Null]': 'null',}
return map[toString.call(data)]
}const t = typeOf(data);
//拿到数据的类型
let result;
if (t === 'array') {//数组
result = [];
for (let i = 0;
i < data.length;
i++){
result.push(deepCopy(data[i]))
}
} else if (t === 'object') {//对象
result = {};
for (let key in data) {
result[key] = deepCopy(data[key])
}
} else {
return data
}
return result;
}
推荐阅读