Created
June 27, 2023 10:55
-
-
Save petsel/8a39ecb7514a577c416aa72bb80b682b 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
// see ... [https://stackoverflow.com/questions/71015428/how-to-get-the-intersection-of-two-sets-while-recognizing-equal-set-values-items/71016510#71016510] | |
// | |
// How to get the intersection of two sets while recognizing | |
// equal set values/items not only by reference but by their | |
// equal structures and entries too? | |
// see also ... [https://stackoverflow.com/questions/76512735/javascript-check-if-arrays-within-an-array-are-the-same] | |
// | |
// Javascript - Check if arrays within an array are the same | |
function isDeepDataStructureEquality(a, b) { | |
let isEqual = Object.is(a, b); | |
if (!isEqual) { | |
if (Array.isArray(a) && Array.isArray(b)) { | |
isEqual = (a.length === b.length) && a.every( | |
(item, idx) => isDeepDataStructureEquality(item, b[idx]) | |
); | |
} else if ( | |
a && b | |
&& (typeof a === 'object') | |
&& (typeof b === 'object') | |
) { | |
const aKeys = Object.keys(a); | |
const bKeys = Object.keys(b); | |
isEqual = (aKeys.length === bKeys.length) && aKeys.every( | |
(key, idx) => isDeepDataStructureEquality(a[key], b[key]) | |
); | |
} | |
} | |
return isEqual; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment