Created
May 26, 2022 03:47
-
-
Save Natumsol/ab3ae3a9341d5e37500c0265153064d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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