Skip to content

result

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

此方法类似于 _.get,只是如果解析的值是一个函数,它会使用其父对象的 this 绑定来调用并返回其结果。

¥This method is like _.get except that if the resolved value is a function it's invoked with the this binding of its parent object and its result is returned.

新增于

¥Since

0.1.0

参数

¥Arguments

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

    ¥object (Object): The object to query.

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

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

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

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

返回

¥Returns

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

¥()*: Returns the resolved value.

示例

¥Example

js
var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };

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

_.result(object, 'a[0].b.c2');
// => 4

_.result(object, 'a[0].b.c3', 'default');
// => 'default'

_.result(object, 'a[0].b.c3', _.constant('default'));
// => 'default'

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