Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active June 6, 2023 15:51
Show Gist options
  • Save ernestlv/d027a2e8b52fda09de4ce2340719946a to your computer and use it in GitHub Desktop.
Save ernestlv/d027a2e8b52fda09de4ce2340719946a to your computer and use it in GitHub Desktop.
The intersection of two sets. It merges objects in common.
/* 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