Skip to content

Instantly share code, notes, and snippets.

@SmashingQuasar
Created September 28, 2022 15:46
Show Gist options
  • Select an option

  • Save SmashingQuasar/fa98beec38a7d76e579d3faf3e999f1f to your computer and use it in GitHub Desktop.

Select an option

Save SmashingQuasar/fa98beec38a7d76e579d3faf3e999f1f to your computer and use it in GitHub Desktop.
Deep object evaluation (TypeScript)
function evaluate(
objectA: Record<number | string | symbol, unknown>,
objectB: Record<number | string | symbol, unknown>,
): boolean {
const keysA: Array<number | string | symbol> = Object.keys(objectA);
const keysB: Array<number | string | symbol> = Object.keys(objectB);
if (keysA.length !== keysB.length) {
return false;
}
for (const key of keysA) {
if (typeof objectA[key] === 'object' && typeof objectB[key] === 'object') {
if (
!evaluate(
<Record<number | string | symbol, unknown>>objectA[key],
<Record<number | string | symbol, unknown>>objectB[key],
)
) {
return false;
}
continue;
}
if (objectA[key] !== objectB[key]) {
return false;
}
}
return true;
}
export { evaluate };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment