Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created February 5, 2018 17:20
Show Gist options
  • Save rafaelrinaldi/cd918e56e4c552b966d1c292e40653a7 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/cd918e56e4c552b966d1c292e40653a7 to your computer and use it in GitHub Desktop.
const combine = (...args) =>
[].slice.call(args).reduce((next, value) => {
// Create an object with duplicates combined
const duplicates = Object.keys(value)
.filter(key => next.hasOwnProperty(key))
.map(key => {
return {
[key]: Object.assign(value[key], next[key])
};
})
.reduce(
(accumulator, current) => Object.assign(accumulator, current),
{}
);
// Create a new object with everything combined together
return Object.assign(next, value, duplicates);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment