Lodash _.defaultsDeep() Method
Lodash _.defaultsDeep() method recursively assigns default properties. It is almost the same as the _.defaults() function. This method mutates the object.
Syntax:
_.defaultsDeep(object, [sources]);
Parameters:
- object: This parameter holds the destination object.
- sources: This parameter holds the source objects.
Return Value:
This method returns the object.
Example 1: In this example, we are using the -.defaultsDeep() method for setting the source’s value to the original object.
Javascript
const _ = require( "lodash" );
let info = {
Name: "GeeksforGeeks" ,
password: "gfg@1234" ,
username: "your_geeks"
}
console.log(_.defaultsDeep(info,
_.defaults(info, { id: 'Id97' })));
|
Output:
{
Name: 'GeeksforGeeks',
password: 'gfg@1234',
username: 'your_geeks',
id: 'Id97'
}
Example 2: In this example, we are using the -.defaultsDeep() method for setting the source’s value to the original object.
Javascript
const _ = require( "lodash" );
console.log(_.defaultsDeep(
{
'x' : { 'y' : 20 }
},
{
'x' : { 'y' : 10, 'z' : 30 }
}
)
);
|
Output:
{ 'x': { 'y': 20, 'z': 30 } }
Last Updated :
20 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...