Skip to content
Related Articles

Related Articles

Lodash _.defaultsDeep() Method

Improve Article
Save Article
  • Last Updated : 09 Sep, 2020
Improve Article
Save Article

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 } }

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!