Skip to content

get

js
_.get(object, path, [defaultValue])

获取 objectpath 的值。如果解析的值是 undefined,则返回 defaultValue 代替它。

¥Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.

新增于

¥Since

3.7.0

参数

¥Arguments

  1. object (对象):的反面;

    ¥object (Object): The object to query.

  2. path (数组|字符串):要获取的属性的路径。

    ¥path (Array|string): The path of the property to get.

  3. [defaultValue] (*):undefined 解析值返回的值。

    ¥[defaultValue] ()*: The value returned for undefined resolved values.

返回

¥Returns

(*):返回解析后的值。

¥()*: Returns the resolved value.

示例

¥Example

js
var object = { 'a': [{ 'b': { 'c': 3 } }] };

_.get(object, 'a[0].b.c');
// => 3

_.get(object, ['a', '0', 'b', 'c']);
// => 3

_.get(object, 'a.b.c', 'default');
// => 'default'

Lodash v4.17 中文网 - 粤ICP备13048890号