Created
April 9, 2021 18:41
-
-
Save whoisryosuke/4d00c21a49bed8992e8bc5ac082d8156 to your computer and use it in GitHub Desktop.
JS - Shallow Equal -- If you're using React Redux, you can also `import { shallowEqual } from "react-redux";`.
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
type CompareObject = { [key: string]: any }; | |
export default function shallowEqual(objA: CompareObject, objB: CompareObject) { | |
if (objA === objB) { | |
return true; | |
} | |
if (!objA || !objB) { | |
return false; | |
} | |
var aKeys = Object.keys(objA); | |
var bKeys = Object.keys(objB); | |
var len = aKeys.length; | |
if (bKeys.length !== len) { | |
return false; | |
} | |
for (var i = 0; i < len; i++) { | |
var key = aKeys[i]; | |
if ( | |
objA[key] !== objB[key] || | |
!Object.prototype.hasOwnProperty.call(objB, key) | |
) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment