Skip to content

Instantly share code, notes, and snippets.

@lcherone
Last active June 27, 2020 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lcherone/a2a60d8566e03a09c799b95e13e930af to your computer and use it in GitHub Desktop.
Save lcherone/a2a60d8566e03a09c799b95e13e930af to your computer and use it in GitHub Desktop.
JavaScript, merge 2 arrays of objects, remove dupes, irrelevant of length, keys or reference
/*
You saw it here first, out of the 8k dupes on stackoverflow, none address diff length b array, diff keys or reference.
Leave a comment if you know a better way
*/
const a = [{ a: 'a' }, { b: 'b' }];
const b = [{ a: 'a' }, { c: 'c' }, { x: 'x' }, {}];
const merge = (a, b) => [
...new Set([
...a.map(i => JSON.stringify(i)),
...b.map(i => JSON.stringify(i))
])
].map(i => JSON.parse(i))
console.log(merge(a, b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment