Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Finding a Key Based on a Property Value:
4.2.
JavaScript
4.3.
Using a Shorthand with Property Value:
4.4.
JavaScript
4.5.
Using a Property Name Shorthand:
4.6.
JavaScript
4.7.
Matching Against a Property Value:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.findKey() differ from _.find()?
5.2.
What happens if no element satisfies the predicate?
5.3.
Can _.findKey() be used with arrays?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.findKey() Method

Author Pallavi singh
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript programming, particularly when dealing with objects, it's often necessary to find the key of the first element that satisfies a certain condition. Lodash's _.findKey() method provides a convenient way to perform this task. 

This method iterates over the properties of an object and returns the key of the first element predicate returns truthy for. It simplifies the process of searching through object properties based on specific criteria.

Why This Function is Used

The _.findKey() function is used to search for a key in an object where its corresponding value meets a certain condition defined by a predicate function. This method is particularly useful in scenarios where you need to locate a specific key based on the properties of its value, especially in objects with numerous or complex properties.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

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

Parameters:

  • object (Object): The object to inspect.
     
  • [predicate=_.identity] (Function): The function invoked per iteration.

Return Value: 

(string|undefined) - Returns the key of the matched element, else undefined.

Examples 

Finding a Key Based on a Property Value:

  • JavaScript

JavaScript

var _ = require('lodash');

var users = {

 'barney':  { 'age': 36, 'active': true },

 'fred':  { 'age': 40, 'active': false },

 'pebbles': { 'age': 1,  'active': true }

};

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

console.log(key);

Output: 

'barney'


Demonstrates finding the key of a user whose age is less than 40.

Using a Shorthand with Property Value:

  • JavaScript

JavaScript

var key = _.findKey(users, { 'age': 1, 'active': true });

console.log(key);

Output: 

'pebbles'


Shows how to use object literal shorthand to find a key.

Using a Property Name Shorthand:

  • JavaScript

JavaScript

var key = _.findKey(users, 'active');

console.log(key);

Output: 

'barney'


An example of using property name shorthand to find the key of the first active user.

Matching Against a Property Value:

  • JavaScript

JavaScript

var key = _.findKey(users, ['active', false]);

console.log(key);

Output:

'fred'


Demonstrates finding the key of a user where the 'active' property is false.

Frequently Asked Questions

How does _.findKey() differ from _.find()?

_.find() returns the value of the first element in a collection that satisfies the predicate, while _.findKey() returns the key of the first property in an object that satisfies the predicate.

What happens if no element satisfies the predicate?

If no element satisfies the predicate, _.findKey() returns undefined.

Can _.findKey() be used with arrays?

While _.findKey() is primarily designed for objects, it can technically be used with arrays. However, the keys in arrays are their indices, so it's more common to use array-specific methods like _.find() for arrays.

Conclusion

Lodash's _.findKey() method offers an efficient and readable way to search for a key in an object based on a condition defined by a predicate function. It is particularly useful for objects with numerous or complex properties where a key needs to be located based on specific criteria.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.