how to use the findlastindex function from lodash in javascript

We can use findLastIndex function from lodash to find the index of the last element in an array for which a given predicate function returns true.

Here's an example of how to use findLastIndex function:

index.tsx
const _ = require('lodash');

const users = [
  { firstName: 'John', lastName: 'Doe', age: 28 },
  { firstName: 'Jane', lastName: 'Doe', age: 32 },
  { firstName: 'Bob', lastName: 'Smith', age: 40 }
];

const lastIndex = _.findLastIndex(users, user => user.age > 30);

console.log(lastIndex); // Output: 1
306 chars
12 lines

In the above example, we are finding the index of the last user object in the array users for which the age is greater than 30. The findLastIndex function returns the index of the matched element, which is 1 in this case.

gistlibby LogSnag