Last active
October 31, 2019 12:41
-
-
Save OrderAndCh4oS/e14f5234088a822299749d06a7c245d0 to your computer and use it in GitHub Desktop.
Combine object arrays without duplicates
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
const combineObjectArrays = (p: any[], q: any[], key = 'id') => { | |
const storedIds: any = []; | |
const arr: any = []; | |
for (let i = 0; i < Math.max(p.length, q.length); i++) { | |
if (i < p.length && !storedIds.includes(p[i][key])) { | |
storedIds.push(p[i][key]); | |
arr.push(p[i]); | |
} | |
if (i < q.length && !storedIds.includes(q[i][key])) { | |
storedIds.push(q[i][key]); | |
arr.push(q[i]); | |
} | |
} | |
return arr; | |
}; | |
export default combineObjectArrays; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment