Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Last active June 6, 2023 15:49
Show Gist options
  • Save ernestlv/f4ef8513ffc962ab60c30aa67ebd82b0 to your computer and use it in GitHub Desktop.
Save ernestlv/f4ef8513ffc962ab60c30aa67ebd82b0 to your computer and use it in GitHub Desktop.
Calculate the difference between two sets
/* it is in A but not in B */
function diff(arrA, arrB) {
const mapB = arrB.reduce((res, b) => ({ ...res, [b.key]:b }), {});
return arrA.reduce((res, a) => !(a.key in mapB) ? [ ...res, a ] : res, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment