Last active
June 18, 2025 09:46
-
-
Save cynthia2006/90c7e2b6f7b2446940729cf1e796b2ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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 setEqual(a, b) { | |
| for (var i = 0; i < a.length; ++i) | |
| if (b.indexOf(a[i]) === -1) | |
| return false; | |
| for (var i = 0; i < b.length; ++i) | |
| if (a.indexOf(b[i]) === -1) | |
| return false; | |
| return true; | |
| } | |
| function objectEqual(a, b) { | |
| if (a === b) | |
| return true; | |
| if (typeof a !== typeof b) | |
| return false; | |
| if (a === null || b === null) | |
| false; | |
| else if(typeof a === "function" || typeof a === "symbol") | |
| false; | |
| var propertiesOfA = Object.getOwnPropertyNames(a) | |
| .concat(Object.getOwnPropertySymbols(a)); | |
| var propertiesOfB = Object.getOwnPropertyNames(b) | |
| .concat(Object.getOwnPropertySymbols(b)); | |
| // Checking if all properties (and symbols) are the same in both objects. | |
| if (!setEqual(propertiesOfA, propertiesOfB)) | |
| return false; | |
| // Checking if the values of all those properties (and symbols) are the same. | |
| for (var prop of propertiesOfA) | |
| if (!objectEqual(a[prop], b[prop])) | |
| return false; | |
| return true; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is this structural equality useful? One great is example is this.