Lodash _.findKey() Method - GeeksforGeeks

10 Sep, 2020
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, lang, function, objects, numbers etc.
The _.findKey() method is similar to _.find() method except that it returns the key of the first element, predicate returns true for instead of the element itself.
Syntax:
_.findKey(object, predicate)
Parameters: This method accepts two parameters as mentioned above and described below:
object: It holds the object to inspect every element.
predicate: It holds the function that the method invoked per iteration.
Return Value: This method returns the key of the matched element else undefined.
Example 1: Here, const _ = require(‘lodash’) is used to import the lodash library in the file.
// Requiring the lodash library const _ = require("lodash");        // Original array var users = {  'meetu':  { 'salary': 36000, 'active': true },  'teetu':  { 'salary': 40000, 'active': false },  'seetu':  { 'salary': 10000,  'active': true }};  // Using the _.findKey() methodlet found_elem = _.findKey(users, function(o) { return   o.salary < 40000; });  // Printing the output console.log(found_elem);
Output:
meetu
Example 2:
// Requiring the lodash library const _ = require("lodash");        // Original array var users = {  'meetu':  { 'salary': 36000, 'active': true },  'teetu':  { 'salary': 40000, 'active': false },  'seetu':  { 'salary': 10000,  'active': true }};  // Using the _.findKey() method// The `_.matches` iteratee shorthandlet found_elem = _.findKey(users, { 'salary': 10000,   'active': true });  // Printing the output console.log(found_elem);
Output:
seetu
Example 3:
// Requiring the lodash library const _ = require("lodash");        // Original array var users = {  'meetu':  { 'salary': 36000, 'active': true },  'teetu':  { 'salary': 40000, 'active': false },  'seetu':  { 'salary': 10000,  'active': true }};  // Using the _.findKey() method// The `_.matchesProperty` iteratee shorthandlet found_elem = _.findKey(users, ['active', false]);  // Printing the output console.log(found_elem);
Output:
teetu
Example 4:
// Requiring the lodash library const _ = require("lodash");        // Original array var users = {  'meetu':  { 'salary': 36000, 'active': true },  'teetu':  { 'salary': 40000, 'active': false },  'seetu':  { 'salary': 10000,  'active': true }};  // Using the _.findKey() method// The `_.property` iteratee shorthandlet found_elem = _.findKey(users, 'active');  // Printing the output console.log(found_elem);
Output:
meetu
Note: This code will not work in normal JavaScript because it requires the library lodash to be installed.
JavaScript-Lodash
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?