nth
arrayGets the element at index n of array. If n is negative, the nth element from the end is returned.
Signature
nth<T>(array: T[], n: number): T | undefinedParameters
| Name | Type | Description |
|---|---|---|
array | T[] | The array to query |
n | number | The index of the element to return |
Returns
T | undefined - Returns the nth element of array
Examples
Positive index
import { nth } from 'dashlite'
const result = nth(['a', 'b', 'c', 'd'], 1)
console.log(result)Output:
'b'Negative index
import { nth } from 'dashlite'
const result = nth(['a', 'b', 'c', 'd'], -2)
console.log(result)Output:
'c'Available since version 1.0.0