Lodash _.defaultsDeep() Method
The _.defaultsDeep() method recursively assigns default properties. It is almost the same as _.defaults() function. This method mutates the object.
Syntax:
_.defaultsDeep(object, [sources])
Parameters: This method accepts two parameters as mentioned above and described below:
- object: This parameter holds the destination object.
- sources: This parameter holds the source objects.
Return Value: This method returns the object.
Example 1:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Given object var info = { Name: "GeeksforGeeks" , password: "gfg@1234" , username: "your_geeks" } // Use of _.defaultsDeep() method console.log(_.defaultsDeep(info, _.defaults(info, { id: 'Id97' }))); |
Output:
{ Name: 'GeeksforGeeks', password: 'gfg@1234', username: 'your_geeks', id: 'Id97' }
Example 2:
Javascript
// Requiring the lodash library const _ = require( "lodash" ); // Use of _.defaultsDeep() method console.log(_.defaultsDeep( { 'x' : { 'y' : 20 } }, { 'x' : { 'y' : 10, 'z' : 30 } } ) ); |
Output:
{ 'x': { 'y': 20, 'z': 30 } }
Please Login to comment...