Skip to content

Instantly share code, notes, and snippets.

@jdelibas

jdelibas/merge.js

Last active Mar 7, 2018
Embed
What would you like to do?
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