_.get()方法用于获取对象路径处的值。如果解析的值未定义,则在其位置返回defaultValue。
用法:
_.get(object, path, [defaultValue])
参数:此方法接受上述和以下所述的三个参数:
- object:此参数保存要查询的对象。
 - path:此参数保存要获取的属性的路径。路径将是数组或字符串。
 - defaultValue:此参数保存为未定义的解析值返回的值。
 
返回值:此方法返回解析的值。
范例1:
Javascript
// Requiring the lodash library   
const _ = require("lodash");   
  
// Given object 
var object = { 'c':[{ 'python':{ 'java':3 } }] }; 
  
// Use of _.get method  
console.log(_.get(object, 'c[0].python.java')); 输出:
3
范例2:
Javascript
// Requiring the lodash library   
const _ = require("lodash");   
  
// Given object 
var object = { 'c':[{ 'python':{ 'java':3 } }] }; 
  
// Use of _.get method  
console.log(_.get(object, ['c', '0', 'python', 'java'])); 输出:
3
范例3:
Javascript
// Requiring the lodash library   
const _ = require("lodash");   
  
// Given object 
var object = { 'c':[{ 'python':{ 'java':3 } }] }; 
  
// Use of _.get method  
console.log(_.get(object, 'c.python.java', 'default')); 输出:
'default'
相关用法
- Lodash _.method()用法及代码示例
 - Lodash _.sneq()用法及代码示例
 - Lodash _.toQuery()用法及代码示例
 - 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()用法及代码示例
 - Lodash _.zipWith()用法及代码示例
 - Lodash _.zipObject()用法及代码示例
 - Lodash _.xor()用法及代码示例
 
注:本文由纯净天空筛选整理自sanjoy_62大神的英文原创作品 Lodash _.get() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
