Last active
April 3, 2025 08:26
-
-
Save paduc/a3e95630ce8cfde35316 to your computer and use it in GitHub Desktop.
Immutable merge for multiple objects (using lodash)
This file contains hidden or 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
merge mutates