Created
May 12, 2017 10:23
-
-
Save du5rte/42b965a03b7237172245a2ea9682885d to your computer and use it in GitHub Desktop.
This file contains 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
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