nth

array

Gets 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 | undefined

Parameters

NameTypeDescription
arrayT[]The array to query
nnumberThe 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