How to use the findLastKey function from lodash

Find comprehensive JavaScript lodash.findLastKey code examples handpicked from public code repositorys.

lodash.findLastKey is a function in the Lodash library that returns the last key of an object for which a provided function returns a truthy value.

2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
*   return chr.age < 40;
* });
* // => returns `pebbles`, assuming `_.findKey` returns `barney`
*
* // using "_.where" callback shorthand
* _.findLastKey(characters, { 'age': 40 });
* // => 'fred'
*
* // using "_.pluck" callback shorthand
* _.findLastKey(characters, 'blocked');
fork icon73
star icon711
watch icon29

+ 5 other calls in file

118
119
120
121
122
123
124
125
126
127
module.exports.find                = _.find;
module.exports.findIndex           = _.findIndex;
module.exports.findKey             = _.findKey;
module.exports.findLast            = _.findLast;
module.exports.findLastIndex       = _.findLastIndex;
module.exports.findLastKey         = _.findLastKey;
module.exports.first               = _.first;
module.exports.firstExisting       = _.firstExisting;
module.exports.fix                 = _.fix;
module.exports.flatMap             = _.flatMap;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.findLastKey work?

lodash.findLastKey is a function in the Lodash library that iterates over the properties of an object from right to left and returns the first key that returns true for the provided predicate function. If none of the keys satisfy the predicate function, it returns undefined. Here is how it works in detail: It takes two arguments: the object to iterate over and the predicate function to be applied to each key. It iterates over the object from right to left. For each key in the object, it applies the predicate function to the value of the key. If the predicate function returns true, it returns the key and exits the loop. If none of the keys satisfy the predicate function, it returns undefined.

632
633
634
635
636
637
638
639
640
641
642
643
644
console.log(extendWith); // => { 'a': 1, 'b': 2, 'c': 3 }


const findKey = _.findKey({ 'a': 1, 'b': 2, 'c': 3 }, o => o === 2);
console.log(findKey); // => 'b'


const findLastKey = _.findLastKey({ 'a': 1, 'b': 2, 'c': 3 }, o => o === 2);
console.log(findLastKey); // => 'b'


const forIn = _.forIn({ 'a': 1, 'b': 2 }, (value, key) => console.log(key));
// => Logs 'a' then 'b'.
fork icon0
star icon4
watch icon0

+ 15 other calls in file

499
500
501
502
503
504
505
506
507
508
509
510
// };


// const filteredUsers = lodash.findKey(users, (ele) => ele.age == 1);
// const filteredUsers = lodash.findKey(users, { age: 40, active: false });
// const filteredUsers = lodash.findKey(users, ["active", false]);
// const filteredUsers = lodash.findLastKey(users, ["active", true]);
// const filteredUsers = lodash.findLastKey(users, { active: true });


// console.log("filteredUsers", filteredUsers);

fork icon0
star icon0
watch icon0

+ 9 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const _ = require("lodash");

const users = {
  user1: { name: "Alice", age: 25 },
  user2: { name: "Bob", age: 30 },
  user3: { name: "Charlie", age: 35 },
  user4: { name: "David", age: 40 },
};

const key = _.findLastKey(users, (user) => (user.age = 30));
console.log(key); // Output: 'user4'

In this example, lodash.findLastKey is used to find the key of the last user in the users object whose age is greater than or equal to 30. The function takes two arguments: the first argument is the object to search through, and the second argument is a function that returns a boolean value indicating whether a given item matches the search criteria. In this case, the search function returns true if the user's age is greater than or equal to 30. The function returns the key of the last matching item, which in this case is 'user4'.

63
64
65
66
67
68
69
70
71
72
var values = valueCounter(hand);

//find the left pair
var leftPair = _.findKey(values, _.curry(_.eq)(2));
//find the right pair
var rightPair = _.findLastKey(values, _.curry(_.eq)(2));
if (leftPair && rightPair && leftPair !== rightPair) {
  //delete the pairs from our object
  delete values[leftPair];
  delete values[rightPair];
fork icon0
star icon0
watch icon0

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)