Created
October 14, 2020 21:26
-
-
Save bsdahl/fb7589c059d25dc17d2aa37abd93702e to your computer and use it in GitHub Desktop.
Function to determine if intersecting keys are equal
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
export function areIntersectingKeysEqual(...objects) { | |
return objects | |
.map((object) => Object.keys(object)) | |
.sort((a, b) => a.length - b.length) | |
.reduce((a, b) => a.filter((key) => b.includes(key))) | |
.every((key) => objects.every((object) => object[key] === objects[0][key])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let me explain how it works.
At a high level, first we find the intersecting keys, then we determine if all intersecting keys are equal.