Skip to content

Instantly share code, notes, and snippets.

@michaelmcshinsky
Last active June 14, 2020 19:46
Show Gist options
  • Save michaelmcshinsky/0a21634bc95d333062f95c1fda1515cd to your computer and use it in GitHub Desktop.
Save michaelmcshinsky/0a21634bc95d333062f95c1fda1515cd to your computer and use it in GitHub Desktop.
let obj1 = { firstName: 'John', lastName: 'Doe', email: 'johnydoe1@domain.com' };
let obj2 = { email: 'john.doe@domain.com', phone: '012–345–6789' };
let obj3 = { city: 'Salt Lake City', state: 'Utah' }
function merge(…arr) {
return arr.reduce((acc, cur) => {
// Can add custom logic for how keys or nested values should be handled.
for (let key in cur) {
acc[key] = cur[key];
}
return acc;
}, {});
}
let newObj = merge(obj1, obj2, obj3);
// {
// firstName: 'John',
// lastName: 'Doe',
// email: 'john.doe@email.com',
// phone: '012–345–6789',
// city: 'Salt Lake City',
// state: 'Utah'
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment