deepClone deepCompare
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
一、深度复制
// 深度复制
function deepClone(datas) {
if (typeof datas !== 'object' || datas === null) return datas;
const newData = new datas.constructor();
for (const key in datas) {
if (Object.hasOwnProperty.call(datas, key)) {
newData[key] = deepClone(datas[key]);
}
}
return newData;
} const datas = {
family: {
father: 'baba',
mother: 'mama',
children: ['brother', 'sister'],
},
mine: {
name: 'shangyy',
hobby: ['basketball', 'football'],
},
}; const cloneDatas = deepClone(datas);
console.log(cloneDatas);
二、深度比较
// 深度比较 function deepCompare(a, b) {
if (a === null || typeof a !== 'object' || a === null || typeof a !== 'object') {
return a === b;
}
const propsA = Object.getOwnPropertyDescriptors(a);
const propsB = Object.getOwnPropertyDescriptors(b);
if (Object.keys(propsA).length !== Object.keys(propsB).length) return false;
return Object.keys(propsA).every(key => deepCompare(a[key], b[key]));
} const dataA = {
name: 'shangyy',
age: 18,
};
const dataB = {
name: 'shangy',
age: 18,
}; const result = deepCompare(dataA, dataB);
console.log(result);
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |