Lodash _.findLastKey() Method
Last Updated :
03 Nov, 2023
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:
- 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: In this example, we are getting the key of the given matched condition and printing the result in the console.
Javascript
const _ = require( 'lodash' );
let 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: In this example, we are getting the key of the given matched condition and printing the result in the console. and returning undefined for values that don’t exist.
Javascript
const _ = require( 'lodash' );
let 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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...