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)); | |
})(); |
Author
bendc
commented
Jul 21, 2016
Does this count as a JSON-like object?
const merged = merge({ a: { a1: true }}, { a: { a2: true }});
console.log(merged); // => { a: { a2: true }}
Wouldn't we want: { a: { a1: true, a2: true }}
?
@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