Friday, September 27, 2024
Google search engine
HomeLanguagesJavascriptLodash _.findLast() Method

Lodash _.findLast() Method

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc.

The _.findLast() method iterates over the elements in a collection from the right to the left. It is almost the same as _.find() method.

Syntax:

_.findLast( collection, predicate, fromIndex )

Parameters: This method accepts three parameters as mentioned above and described below:

  • collection: It is the collection that the method iterates over.
  • predicate: It is the function that is invoked for every iteration.
  • fromIndex: It is the index of the array from where the search starts.

Return Value: This method returns the element that matches, else undefined.

Example 1:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var users = ([3, 4, 5, 6]);
  
// Using the _.findLast() method
let found_elem = _.findLast(users, function(n) {
  return n % 2 == 1;
});
  
// Printing the output 
console.log(found_elem);


Output:

5

Example 2:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var user1 = ([3, 4, 5, 6, 9, 1, 7]);
var user2 = ([24, 14, 55, 36, 76]);
  
// Using the _.findLast() method
let found_elem = _.findLast(user1, function(n) {
  return n % 2 == 0;
});
let found_elem2 = _.findLast(user2, function(n) {
  return n % 2 == 1;
});
  
// Printing the output 
console.log(found_elem);
console.log(found_elem2);


Output:

6
55

Example 3:




// Requiring the lodash library 
const _ = require("lodash"); 
      
// Original array 
var user1 = ([3.5, 4.7, 5.8, 6.9, 9.4, 1.3, 7.2]);
  
// Using the _.findLast() method
let found_elem = _.findLast(user1, function(n) {
  return n % 2 == 1;
});
  
// Printing the output 
console.log(found_elem);


Output:

undefined

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments