Is there any way to get Underscore.js extend
function:
Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.
... to work recursively?
Actually, query
property in creditOperation
is going to completely override the query
property defined in baseOperation
:
var url = require('url')
, _ = require('underscore'),
, baseOperation = {
host: 'gateway.skebby.it',
pathname: 'api/send/smseasy/advanced/http.php',
protocol: 'https',
query: {
'username': 'foo',
'password': 'bar',
}
};
var creditOperation = _.extend(baseOperation, {
query: {
'method': 'baz'
}
});
console.log(url.format(creditOperation));
I'd like to obtain this creditOperation
:
{
host: 'gateway.skebby.it',
pathname: 'api/send/smseasy/advanced/http.php',
protocol: 'https',
query: {
'username': 'foo',
'password': 'bar',
'method': 'baz'
}
}
Kurt Milam has published a mixin that adds a
deepExtend
method to underscore.js. It even deals with regular expressions (if you want). Excerpt from the documentation:jQuery has an extend() function, which does the same thing as its Underscore counterpart, but also has a deep argument which allows it to merge recursively as you desire:
Or, if you don't want to overwrite baseOperation:
No, Underscore will not contain a deep extend since it's too complicated to deal with different types of objects. Instead, users are encouraged to implement their own solutions with the support for what they need.
In your case it's only plain objects, so an implementation is quite straightforward:
Stand-alone version of Bergi's deep extend, including the fix for when a value is a string instead of an object. Also patched to be more strict.
With Lodash (fork of underscore) u can. Lodash's _.extend method accept third (or higher) parameter to be a function, that receives values (old and new); So u can do something like this:
upd. As Paolo Moretti said in comments, there is the same function in lodash called _.merge:
underscore's extend() does not do deep extend; as a matter of fact, there is no function in underscore which can deep extend.
You may use lodash's merge for that.