Use to get an element from an array
In this approach, we are using the nth method with an array to retrieve the element present at the specified index passed as the second argument to the method.
Syntax:
_.nth(array, index);
Example: The below example uses the nth Method to get an element from an array.
const _ = require('lodash');
const array =
["JavaScript", "GFG", 3, 5];
const elementAtIndex2 =
_.nth(array, 1);
console.log(elementAtIndex2);
Output:
GFG
How to use the nth method in Lodash ?
The nth Method in Lodash is used to access elements or characters at specific indices within arrays or strings in JavaScript. You can implement the nth method to perform the below operations in Lodash.
Table of Content
- Use to get an element from an array
- Use to get a character from string
Run the below command before running the code examples on your local machines:
npm i lodash
Contact Us