Last active
June 14, 2020 19:46
-
-
Save michaelmcshinsky/0a21634bc95d333062f95c1fda1515cd 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
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