Last active
March 7, 2018 16:47
-
-
Save skelet00r/65ba0b0910e88bf1bbed1a88e74e09d8 to your computer and use it in GitHub Desktop.
merge object array via discriminator - append only
This file contains hidden or 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
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