Thursday, October 23, 2025
HomeLanguagesJavascriptLodash _.findLastKey() Method

Lodash _.findLastKey() Method

The Lodash _.findLastKey() Method is like the _.findKey() method except that it iterates over elements of a collection in the opposite order.

Syntax:

_.findLastKey( object, predicate_function)

Parameters: This method accepts two parameters as mentioned above and described below:

  • object: This parameter holds the object to find in.
  • predicate_function: Function that is invoked per iteration.

Return Value: This method returns the key of the matched element, else undefined.

Example 1:

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var users = {
  'Ram':  { 'mark': 100, 'status': "pass" },
  'Shyam': { 'mark': 90, 'status': "pass" },
  'Arnav': { 'mark': 50,  'status': "fail" }
};
   
console.log(_.findLastKey(users, function(s) 
                { return s.mark > 80; }));
   
console.log(_.findLastKey(users, 
        { 'mark': 100, 'status': "pass" }));
  
console.log(_.findLastKey(users, 
        ['status', "fail"]));
   
console.log(_.findLastKey(users, 'status'));


Output:

Shyam
Ram
Arnav
Arnav

Example 2: returns undefined for values that don’t exist.

Javascript




// Defining Lodash variable 
const _ = require('lodash'); 
  
var users = {
  'Ram':  { 'mark': 100, 'status': "pass" },
  'Shyam': { 'mark': 90, 'status': "pass" },
  'Arnav': { 'mark': 50,  'status': "fail" }
};
   
console.log(_.findLastKey(users, function(s) 
        { return false; }));
   
console.log(_.findLastKey(users, 
        { 'mark': 100, 'status': "fail" }));
  
console.log(_.findLastKey(users, ['status', ""]));
   
console.log(_.findLastKey(users, 'mark'));


Output:

undefined
undefined
undefined
Arnav

Note: This will not work in normal JavaScript because it requires the lodash library to be installed and can be installed using npm install lodash.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS