Monday, November 18, 2024
Google search engine
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

Recent Comments