Lodash - property 方法

语法

_.property(path)

创建一个函数,返回给定对象路径的值。

参数

  • path (Array|string) − 要获取的属性的路径。

输出

  • (Function) − 返回新的访问器函数。

示例

var _ = require('lodash');
var objects = [
   { 'a': { 'b': 2 } },
   { 'a': { 'b': 1 } }
];
 
var result = _.map(objects, _.property('a.b'));
console.log(result); 
result = _.map(_.sortBy(objects, _.property(['a', 'b'])), 'a.b');
console.log(result);

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

[ 2, 1 ]
[ 1, 2 ]

lodash_util.html