Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const stateFiles = [
{name: 'john'},
{name: 'lewis'},
]
const files = [
{name: 'ana'},
{name: 'john', email: "john@mail.com"},
]
function mergeObjectArraysByName(prev, next) {
return prev.reduce((acc, prevItem, i) => {
const exists = acc.find(nextItem => prevItem.name === nextItem.name);
return exists ? acc : [prevItem,...acc];
}, next);
}
const newFiles = mergeObjectArraysByName(stateFiles, files)
console.log(newFiles)
console.log(newFiles.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment