Skip to content

Instantly share code, notes, and snippets.

@paduc
Last active December 3, 2020 22:39
Show Gist options
  • Save paduc/a3e95630ce8cfde35316 to your computer and use it in GitHub Desktop.
Save paduc/a3e95630ce8cfde35316 to your computer and use it in GitHub Desktop.
Immutable merge for multiple objects (using lodash)
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)));
}
};
@ngothanhtai
Copy link

Thanks @agarb7. I like your approach.

@derek-adair
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment