Lodash - result 方法
语法
_.result(object, path, [defaultValue])
此方法类似于 _.get,不同之处在于,如果解析的值是一个函数,则使用其父对象的 this 绑定来调用它并返回其结果。
参数
object (Object) − 要查询的对象。
path (Array|string) − 要解析的属性的路径。
[defaultValue] (*) −未定义解析值的返回值。
输出
(*) − 返回解析值。
示例
var _ = require('lodash'); var object = { 'a': [{ 'b': { 'c1': 3 } }] }; var result = _.result(object, 'a[0].b.c1'); console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
3