Created
May 16, 2025 13:09
-
-
Save andersonbosa/9155cf7a1d9dcd513df9e705b3942b5b 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
| /** | |
| * Returns elements that exist in both groups based on a key comparison | |
| * @param groupA First array of objects to compare | |
| * @param groupB Second array of objects to compare | |
| * @param key Property name to use for comparison | |
| * @returns Array of elements from groupA that have matching key values in groupB | |
| */ | |
| function groupsIntersection(groupA: any[], groupB: any[], key: string) { | |
| return groupA.filter(item => groupB.some(b => b[key] === item[key])) | |
| } | |
| /** | |
| * Returns elements from groupA that don't exist in groupB based on a key comparison | |
| * @param groupA Array to find unique elements from | |
| * @param groupB Array to compare against | |
| * @param key Property name to use for comparison | |
| * @returns Array of elements from groupA that don't have matching key values in groupB | |
| */ | |
| function groupsDifference(groupA: any[], groupB: any[], key: string) { | |
| return groupA.filter(item => !groupB.some(b => b[key] === item[key])) | |
| } | |
| /** | |
| * Combines both groups while removing duplicates based on a key comparison | |
| * @param groupA First array to combine | |
| * @param groupB Second array to combine | |
| * @param key Property name to use for comparison | |
| * @returns Combined array with duplicates removed | |
| */ | |
| function groupsUnion(groupA: any[], groupB: any[], key: string) { | |
| return [...groupA, ...groupB.filter(item => !groupA.some(a => a[key] === item[key]))] | |
| } | |
| /** | |
| * Returns elements that exist in one group but not both (XOR operation) | |
| * @param groupA First array to compare | |
| * @param groupB Second array to compare | |
| * @param key Property name to use for comparison | |
| * @returns Array of elements unique to either group but not both | |
| */ | |
| function groupsSymmetricDifference(groupA: any[], groupB: any[], key: string) { | |
| return [...groupA.filter(item => !groupB.some(b => b[key] === item[key])), ...groupB.filter(item => !groupA.some(a => a[key] === item[key]))] | |
| } | |
| /** | |
| * Creates all possible pairs between elements of both groups | |
| * @param groupA First array to combine | |
| * @param groupB Second array to combine | |
| * @param key Property name (unused in this operation but kept for consistency) | |
| * @returns Array of objects containing pairs of elements from both groups | |
| */ | |
| function groupsCartesianProduct(groupA: any[], groupB: any[], key: string) { | |
| return groupA.flatMap(a => groupB.map(b => ({ a, b }))) | |
| } | |
| function main() { | |
| const groupA = [ | |
| { id: 1, name: 'John' }, | |
| { id: 2, name: 'Jane' }, | |
| { id: 3, name: 'Jim' } | |
| ] | |
| const groupB = [ | |
| { id: 2, name: 'Jane' }, | |
| { id: 3, name: 'Jim' }, | |
| { id: 4, name: 'Jill' }, | |
| { id: 5, name: 'John' } | |
| ] | |
| console.log(groupsIntersection(groupA, groupB, 'name')) | |
| // console.log(groupsDifference(groupA, groupB, 'name')) | |
| // console.log(groupsUnion(groupA, groupB, 'name')) | |
| // console.log(groupsSymmetricDifference(groupA, groupB, 'name')) | |
| // console.log(groupsCartesianProduct(groupA, groupB, 'name')) | |
| } | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment