Skip to content

Instantly share code, notes, and snippets.

@olegpolyakov
Created April 29, 2020 09:18
Show Gist options
  • Save olegpolyakov/be14b7c3647af0300ef2e269453dec82 to your computer and use it in GitHub Desktop.
Save olegpolyakov/be14b7c3647af0300ef2e269453dec82 to your computer and use it in GitHub Desktop.
dedupMerge
const people = [
{
department: "Directing",
id: 20561,
job: "Director",
name: "James Gray"
},
{
department: "Writing",
id: 20561,
job: "Screenplay",
name: "James Gray"
},
{
department: "Writing",
id: 1750900,
job: "Screenplay",
name: "Ethan Gross"
},
{
department: "Writing",
id: 20561,
job: "Dresser",
name: "James Gray"
}
];
function dedupMerge(items, map) {
return Object.values(people.reduce((result, item) => {
result[item.id] = !result[item.id] ? item : map(result[item.id], item);
return result;
}, {}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment