Skip to content

Instantly share code, notes, and snippets.

@jdelibas
Last active March 7, 2018 16:47
Show Gist options
  • Save jdelibas/65ba0b0910e88bf1bbed1a88e74e09d8 to your computer and use it in GitHub Desktop.
Save jdelibas/65ba0b0910e88bf1bbed1a88e74e09d8 to your computer and use it in GitHub Desktop.
merge object array via discriminator - append only
function merge(dest, src, discriminator) {
if (!discriminator) {
return [...dest, ...src];
}
const result = [...dest];
src.forEach((s) => {
const match = dest.find(d => d[discriminator] === s[discriminator]);
if (match) {
const oldVal = JSON.stringify(match);
const newVal = JSON.stringify(s);
if (oldVal !== newVal) {
const index = result.indexOf(match);
result[index] = s;
return;
}
return;
}
result.push(s);
});
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment