Lodash | _.nth() Method
The _.nth() method is used to return the nth index of the element. For a negative value of n, it returns the nth element from the end.
Parameters: This method accepts two parameters as mentioned above and described below:
- Array: This parameter holds the query array.
- n: This parameter holds the index of element that to be extracted.
Return Value: It returns the nth element of the array.
Example 1: It returns the third element of an array.
const _ = require('lodash') is used to import the lodash library into the file.
Example 2: It returns the third element from the end of an array because the value of n is negative.
Example 3: It returns undefined because there is no element at index 8.