Arrays.next()

Syntax

function next<T>(array: readonly T[], fromIndex: number): T | undefined

Gets the next element in the array, starting at the given index.

Parameters
array

An array with any number of elements.

fromIndex

An in-bounds index. The function will return the element belonging to fromIndex + 1, or 0 if fromIndex is the last index in the array.

Example

import { Arrays } from 'potence';

const array = [3, 1, 5];

console.log(Arrays.next(array, 0));   // -> 1
console.log(Arrays.next(array, 2));   // -> 3

Remarks

This function will “wrap”, that is it will return the first element if the passed index belongs to the last element in the array.

The index must be within the bounds of the array. If it isn’t, the function returns undefined. In TypeScript, you can use the non-null assertion operator (!) on the return value if you are absolutely certain the index is in-bounds.

This function is analogous to Arrays.previous().