Lodash - findKey method


Advertisements

Syntax

_.findKey(object, [predicate=_.identity])

This method is like _.find except that it returns the key of the first element predicate returns truthy for instead of the element itself.

Arguments

  • object (Object) − The object to inspect.

  • [predicate=_.identity] (Function) − The function invoked per iteration.

Output

  • (*) − Returns the key of the matched element, else undefined.

Example

var _ = require('lodash');
var users = {
   'Joe': { 'age': 36, 'active': true },
   'Robert': { 'age': 40, 'active': false },
   'Stefan': { 'age': 10,   'active': true }
};

var result = _.findKey(users, function(o) { return o.age < 40; });
console.log(result);

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

Command

\>node tester.js

Output

Joe
lodash_object.htm
Advertisements