Lodash _.last() Method
Lodash is a JavaScript library that works on the top of Underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.last() method is used to get the last element of the array i.e. (n-1)th element.
Syntax:
lodash.last( array )
Parameters: This function accepts single parameter i.e. the array.
Return Value: It returns the last element of the array.
Note: Please install lodash module by npm install lodash before using the below given code.
Example 1:
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array let array1 = [ "a" , "b" , "c" , "d" , "e" ] // Using _.last() method let value = _.last(array1); // Printing original Array console.log( "original Array1: " , array1) // Printing the value console.log( "value: " , value) |
Output:
Example 2:
javascript
// Requiring the lodash library const _ = require( "lodash" ); // Original array let array1 = [] // Using _.last() method let value = _.last(array1); // Printing original Array console.log( "original Array1: " , array1) // Printing the value console.log( "value: " , value) |
Output:
Please Login to comment...