Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created May 26, 2022 03:47
Show Gist options
  • Save Natumsol/ab3ae3a9341d5e37500c0265153064d8 to your computer and use it in GitHub Desktop.
Save Natumsol/ab3ae3a9341d5e37500c0265153064d8 to your computer and use it in GitHub Desktop.
function shallowEqual(objA, objB) {
if (objA === objB) {
return true;
}
if (
!(typeof objA === 'object' && objA != null) ||
!(typeof objB === 'object' && objB != null)
) {
return false;
}
const keysA = Object.keys(objA);
const keysB = Object.keys(objB);
if (keysA.length !== keysB.length) {
return false;
}
for (let i = 0; i < keysA.length; i++) {
if (objB.hasOwnProperty(keysA[i])) {
if (objA[keysA[i]] !== objB[keysA[i]]) {
console.log('不相等的Key', keysA[i]);
return false;
}
} else {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment