Lodash - property method


Advertisements

Syntax

_.property(path)

Creates a function that returns the value at path of a given object.

Arguments

  • path (Array|string) − The path of the property to get.

Output

  • (Function) − Returns the new accessor function.

Example

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);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

[ 2, 1 ]
[ 1, 2 ]
lodash_util.htm
Advertisements