How to use the findLast function from lodash

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

lodash.findLast is a method in the Lodash library that searches for the last element in an array that matches a given predicate.

4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
*  to create a "_.pluck" or "_.where" style callback, respectively.
* @param {*} [thisArg] The `this` binding of `callback`.
* @returns {*} Returns the found element, else `undefined`.
* @example
*
* _.findLast([1, 2, 3, 4], function(num) {
*   return num % 2 == 1;
* });
* // => 3
*/
fork icon73
star icon711
watch icon29

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

+ 92 other calls in file

How does lodash.findLast work?

lodash.findLast is a method provided by the Lodash library that iterates over elements of a collection from right to left and returns the first element that satisfies a given condition. It is similar to lodash.find, but it iterates from the end of the collection to the beginning, rather than the other way around. If no element satisfies the condition, it returns undefined.

115
116
117
118
119
120
121
122
123
124
125
126
127


  return events;
};


const checkError = (events) => {
  const error = _.findLast(events, (elem) => elem.type === 'ExecutionFailed');


  if (error) {
    return error.executionFailedEventDetails;
  }
fork icon21
star icon23
watch icon3

+ 7 other calls in file

217
218
219
220
221
222
223
224
225
226
227
228
229
console.log(filter); // => [4, 6]


const find = _.find([4, 5, 6], n => n % 2 == 0);
console.log(find); // => 4


const findLast = _.findLast([4, 5, 6], n => n % 2 == 0);
console.log(findLast); // => 6


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

+ 15 other calls in file

Ai Example

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

const numbers = [1, 3, 5, 2, 4, 6];

const lastEven = _.findLast(numbers, (n) => n % 2 === 0);

console.log(lastEven); // Output: 6

In this example, lodash.findLast is used to search through the numbers array from right-to-left until the first even number is found. The function passed as the second argument returns true for even numbers, and false for odd numbers. The return value of lodash.findLast is the last element in the array for which the function returns true, which in this case is 6.

366
367
368
369
370
371
372
373
374
375
    });
  }
};

openaps.findOfflineMarker = function findOfflineMarker (sbx) {
  return _.findLast(sbx.data.treatments, function match (treatment) {
    var eventTime = sbx.entryMills(treatment);
    var eventEnd = treatment.duration ? eventTime + times.mins(treatment.duration).msecs : eventTime;
    return eventTime <= sbx.time && treatment.eventType === 'OpenAPS Offline' && eventEnd >= sbx.time;
  });
fork icon0
star icon1
watch icon1

+ 3 other calls in file

758
759
760
761
762
763
764
765
766

let tsIncrement = 0;

for(let modelDef of modelDefs){

    const lastFile = _.findLast(allFiles, (fl) => {
        const [tsString] = _.split(fl.name, '_', 1);
        return fl.name.substring(tsString.length) === `_${modelDef.tableName}.js`;
    });
fork icon0
star icon0
watch icon2

1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
  lookup = new RegExp(`(?:/|^)${escapeStrRe(lookup)}(?=/)`, 'g');
  ns = ns.replace(lookup, '');
}

const folders = ns.split('/');
const scope = _.findLast(folders, folder => folder.indexOf('@') === 0);

// Cleanup `ns` from unwanted parts and then normalize slashes to `:`
ns = ns
  .replace(/\/\//g, '') // Remove double `/`
fork icon0
star icon0
watch icon1

54
55
56
57
58
59
60
61
62
63
64
65
66
67
// find & findLast


var found = _.find(users, o => o.active);
// log("first found", found);


found = _.findLast(users, o => o.active);
// log('last found', found);





fork icon0
star icon0
watch icon0

129
130
131
132
133
134
135
136
137
sbx.isCurrent = function isCurrent (entry) {
  return entry && sbx.time - entry.mills <= times.mins(15).msecs;
};

sbx.lastEntry = function lastEntry (entries) {
  return _.findLast(entries, function notInTheFuture (entry) {
    return sbx.entryMills(entry) <= sbx.time;
  });
};
fork icon0
star icon0
watch icon1

Other functions in lodash

Sorted by popularity

function icon

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