A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-defaults-method/ below:

Lodash _.defaults() Method - GeeksforGeeks

Lodash _.defaults() Method

Last Updated : 30 Oct, 2023

Lodash _.defaults() method assigns properties of source objects to the destination object for all destination properties that resolve to undefined. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored. This method mutates the object.

Syntax:
_.defaults( dest_object, [src_obj]);
Parameters: Return Value:

This method returns an object.

Example 1: In this example, we are using the lodash _.defaults() method in which we are passing the source and destination object and we are getting the result.

javascript
// Defining Lodash variable 
const _ = require('lodash');

let a = _.defaults({ 'gfg': 3 },
    { 'geek': 1 }, { 'gfg': 6 });

console.log(a);

Output:

{ gfg: 3, geek: 1 }

Example 2: In this example, we are using the lodash _.defaults() method in which we are passing the source and destination object and we are getting the result.

javascript
// Defining Lodash variable 
const _ = require('lodash');

let a = _.defaults({ 'a': 3 }, { 'b': 1 },
    { 'c': 5 }, { 'd': 5 }, { 'e': 5 });

console.log(a);

Output:

{ a: 3, b: 1, c: 5, d: 5, e: 5 }

Example 3: In this example, we are using the lodash _.defaults() method in which we are passing the source and destination object and we are getting the result.

javascript
// Defining Lodash variable 
const _ = require('lodash');

let a = _.defaults({ 'a': 'first setting' },
    { 'a': 'second setting but doesnt changes' });
    
console.log(a);

Output:

{ a: 'first setting' }                                                                                                                                                                                                                                                                                          

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using the following command:

npm install lodash


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4