Last Updated : 02 Sep, 2024
Lodash _.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.
Syntax:_.nth(array, n);Parameters:
Example 1: In this example, It returns the third element of an array.
javascript
const _ = require('lodash');
let ar = [1, 2, 3, 4, 5]
let value = _.nth(ar, 3)
console.log(value)
Output:
4
Example 2: In this example, It returns the third element from the end of an array because the value of n is negative.
javascript
const _ = require('lodash');
let ar = [1, 2, 3, 4, 5]
let value = _.nth(ar, -3)
console.log(value)
Output:
3
Example 3: In this example, It returns undefined because there is no element at index 8.
javascript
const _ = require('lodash');
let ar = [1, 2, 3, 4, 5]
let value = _.nth(ar, 8)
console.log(value)
Output:
undefined
Reference: https://lodash.com/docs/4.17.15#nth
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4