Skip to content

Instantly share code, notes, and snippets.

@OrderAndCh4oS
Last active October 31, 2019 12:41
Show Gist options
  • Save OrderAndCh4oS/e14f5234088a822299749d06a7c245d0 to your computer and use it in GitHub Desktop.
Save OrderAndCh4oS/e14f5234088a822299749d06a7c245d0 to your computer and use it in GitHub Desktop.
Combine object arrays without duplicates
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