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.