Created
July 21, 2016 14:15
-
-
Save bendc/1726e1ad49723d7df1d0dc09bd560421 to your computer and use it in GitHub Desktop.
Deep merge of JSON-like objects
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 merge = (() => { | |
const duplicate = object => JSON.parse(JSON.stringify(object)); | |
return (...objects) => Object.assign(...objects.map(duplicate)); | |
})(); |
@cuth I think both results could be reasonable. I personally prefer the behavior of Object.assign
(i.e. the last one wins) as it lets you easily override previous properties (useful for example when dealing with state objects).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this count as a JSON-like object?
Wouldn't we want:
{ a: { a1: true, a2: true }}
?