Is there an alternative method available in lodash, underscore or other library that almost behaves the same way, except that it returns a new object instead of mutating the first argument?.
var o = { 'user': 'barney' }
var result = method(o, { 'age': 40 }, { 'user': 'fred' })
// o still { 'user': 'barney' }
// result is now { 'user': 'fred', 'age': 40 }
The _. assign method will only assign the own enumerable properties from source objects, and will not do anything with inherited properties and methods from a prototype when dealing with objects that are created from a class.
Because Lodash is updated more frequently than Underscore. js, a lodash underscore build is provided to ensure compatibility with the latest stable version of Underscore.
The functional programming variant of lodash assign does not mutate no arguments :) Use merge (the FP version of merge is also immutable) for recursively merging nested objects.
The most common way of doing this seems to use an empty object and assign onto that, like:
var result = _.assign({}, l, m, n, o, p);
This is not technically immutable but will produce a "new" object that did not exist before the function was called.
Bear in mind that even a very clever implementation of clone would have to do this same thing. It's trivial to create the new object manually, so most libraries don't worry about a helper for this case. The next closest thing would be _.create
, which has more to do with assigning the correct prototype.
I like defaults() for cases like this.
var user = { user: 'barney', age: 36 };
_.defaults({ age: 40 }, user);
// → { user: 'barney', age: 40 }
user;
// → { user: 'barney', age: 36 }
The first argument is the destination, and user
isn't mutated. I like using defaults()
when I need to override properties, as is the case here with age
, but don't want to actually change anything in the original. Because defaults()
will only add properties that resolve to undefined
. The age
property exists in the object literal, so it's value is kept.
The assign()
approach works just as well - defaults()
is just a different way to think about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With