How to use the findLastIndex function from lodash

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

lodash.findLastIndex is a function in the Lodash library that searches an array in reverse order and returns the index of the last element that satisfies a provided condition.

5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
*   return chr.age > 30;
* });
* // => 1
*
* // using "_.where" callback shorthand
* _.findLastIndex(characters, { 'age': 36 });
* // => 0
*
* // using "_.pluck" callback shorthand
* _.findLastIndex(characters, 'blocked');
fork icon73
star icon711
watch icon29

+ 5 other calls in file

117
118
119
120
121
122
123
124
125
126
module.exports.filter              = _.filter;
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;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

How does lodash.findLastIndex work?

lodash.findLastIndex is a function in the Lodash library that iterates over an array in reverse order and returns the index of the last element that matches the provided callback function.

39
40
41
42
43
44
45
46
47
48
49
50
51
console.log(fillWith); // => [2, 2, 2]


const findIndex = _.findIndex([1, 2, 3, 4], n => n % 2 === 1);
console.log(findIndex); // => 0


const findLastIndex = _.findLastIndex([1, 2, 3, 4], n => n % 2 === 1);
console.log(findLastIndex); // => 2


const firstHead = _.first([1, 2, 3]);
console.log(firstHead); // => 1
fork icon0
star icon4
watch icon0

+ 15 other calls in file

136
137
138
139
140
141
142
143
144
145
let newIndex = index
if (index === undefined) {
  if (item.split("/").length === 2) {
    // if file in subfolder, get index of last file in subfolder
    newIndex =
      _.findLastIndex(
        content.collections[collectionName].order,
        (f) => f.split("/")[0] === item.split("/")[0]
      ) + 1
  } else {
fork icon0
star icon3
watch icon2

+ 6 other calls in file

Ai Example

1
2
3
4
5
6
const _ = require("lodash");

const arr = [5, 10, 15, 20, 25, 30];
const index = _.findLastIndex(arr, (num) => num > 20);

console.log(index); // Output: 5

In this example, we're using lodash.findLastIndex to search for the last element in the arr array that is greater than 20. The function returns the index of that element in the array, which is 5 in this case.

265
266
267
268
269
270
271
272
273
274

// AMP template has style injected directly because there can only be one <style amp-custom> tag
if (options.data.site.accent_color && !_.includes(context, 'amp')) {
    const accentColor = escapeExpression(options.data.site.accent_color);
    const styleTag = `<style>:root {--ghost-accent-color: ${accentColor};}</style>`;
    const existingScriptIndex = _.findLastIndex(head, str => str.match(/<\/(style|script)>/));

    if (existingScriptIndex !== -1) {
        head[existingScriptIndex] = head[existingScriptIndex] + styleTag;
    } else {
fork icon0
star icon0
watch icon1

+ 3 other calls in file

53
54
55
56
57
58
59
60
61
62
63
// console.log(_.findLastIndex(objects3, ['year', 2019]));
// console.log(_.findIndex(objects3, ['year', 2016]));
// console.log(_.findLastIndex(objects3, ['year', 2016]));


// console.log(
//   _.findLastIndex(objects3, function (t) {
//     return t.year === 2019;
//   })
// );

fork icon0
star icon0
watch icon0

+ 2 other calls in file

156
157
158
159
160
161
162
163
164
165
let users4 = [
    { 'user': 'barney', 'active': true },
    { 'user': 'fred', 'active': false },
    { 'user': 'pebbles', 'active': false }
];
let findLastIndex1 = _.findLastIndex(users4, function (o) { return o.user == 'pebbles' });
console.log('findLastIndex1--->', findLastIndex1);
//findLastIndex1---> 2
let findLastIndex2 = _.findLastIndex(users4, { 'user': 'barney', 'active': true });
console.log('findLastIndex2--->', findLastIndex2);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

Other functions in lodash

Sorted by popularity

function icon

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