Skip to content

Instantly share code, notes, and snippets.

@petsel
Created June 27, 2023 10:55
Show Gist options
  • Save petsel/8a39ecb7514a577c416aa72bb80b682b to your computer and use it in GitHub Desktop.
Save petsel/8a39ecb7514a577c416aa72bb80b682b to your computer and use it in GitHub Desktop.
// 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