Skip to content

Instantly share code, notes, and snippets.

@du5rte
Created May 12, 2017 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save du5rte/42b965a03b7237172245a2ea9682885d to your computer and use it in GitHub Desktop.
Save du5rte/42b965a03b7237172245a2ea9682885d to your computer and use it in GitHub Desktop.
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