Lodash - property 方法



語法

_.property(path)

建立返回給定物件 path 處值的函式。

引數

  • path (Array|string) − 要獲取的屬性的 path。

輸出

  • (函式) − 返回新的訪問器函式。

示例

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.htm
廣告
© . All rights reserved.