Last active
June 27, 2020 00:15
-
-
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
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
/* | |
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