Last active
December 3, 2020 22:39
-
-
Save paduc/a3e95630ce8cfde35316 to your computer and use it in GitHub Desktop.
Immutable merge for multiple objects (using lodash)
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
var _ = require('lodash'); | |
function immutableMerge(){ | |
if(arguments.length === 0) return {}; | |
if(arguments.length === 1) return arguments[0]; | |
if(arguments.length === 2) { | |
return _.merge(_.cloneDeep(arguments[0]), arguments[1]); | |
} | |
else{ | |
return immutableMerge(_.first(arguments), immutableMerge(_.rest(arguments))); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Was looking for a way to join multiple style objects in React. It ended up looking like this.