Lodash _.defaults()方法將解析為未定義的所有目標屬性的源對象屬性分配給目標對象。源對象從左到右應用。設置屬性後,將忽略同一屬性的其他值。此方法使對象變異。
用法:
_.defaults( dest_object, [src_obj])
參數:該方法接受上述和以下所述的兩個參數:
- dest_object:這是目標對象。
- src_obj:這些是源對象。
返回值:此方法返回一個對象。
例子1:
// Defining Lodash variable
const _ = require('lodash');
a = _.defaults({ 'gfg':3 },
{ 'geek':1 }, { 'gfg':6 });
console.log(a);
輸出:
{ gfg:3, geek:1 }
範例2:
// Defining Lodash variable
const _ = require('lodash');
a = _.defaults({ 'a':3 }, { 'b':1 },
{ 'c':5 }, { 'd':5 }, { 'e':5 });
console.log(a);
輸出:
{ a:3, b:1, c:5, d:5, e:5 }
範例3:
// Defining Lodash variable
const _ = require('lodash');
a = _.defaults({ 'a':'first setting'},
{ 'a':'second setting but doesn't changes'});
console.log(a);
輸出:
{ a:'first setting' }
注意:這在正常的JavaScript中將不起作用,因為它需要安裝lodash庫,並且可以使用以下命令進行安裝:
npm install lodash
。
相關用法
- Underscore.js _.defaults()用法及代碼示例
- Lodash _.method()用法及代碼示例
- Lodash _.uniqWith()用法及代碼示例
- Lodash _.xorWith()用法及代碼示例
- Lodash _.head()用法及代碼示例
- Lodash _.remove()用法及代碼示例
- Lodash _.pullAt()用法及代碼示例
- Lodash _.pullAll()用法及代碼示例
- Lodash _.pull()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.takeRight()用法及代碼示例
- Lodash _.take()用法及代碼示例
- Lodash _.sortedLastIndex()用法及代碼示例
- Lodash _.fromPairs()用法及代碼示例
- Lodash _.differenceWith()用法及代碼示例
- Lodash _.castArray()用法及代碼示例
- Lodash _.cloneDeep()用法及代碼示例
- Lodash _.clone()用法及代碼示例
- Lodash _.sampleSize()用法及代碼示例
- Lodash _.find()用法及代碼示例
注:本文由純淨天空篩選整理自taran910大神的英文原創作品 Lodash _.defaults() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。