Last active
June 6, 2023 15:51
-
-
Save ernestlv/d027a2e8b52fda09de4ce2340719946a to your computer and use it in GitHub Desktop.
The intersection of two sets. It merges objects in common.
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
/* it is in both A & B */ | |
/* merge common objects using expression: { ...map[b.key], ...b } */ | |
function intersection(arrA, arrB) { | |
const mapA = arrA.reduce((res, a) => ({ ...res, [a.key]:a }), {}); | |
return arrB.reduce((res, b) => (b.key in mapA) ? [ ...res, { ...mapA[b.key], ...b } ] : res, []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment