last
Get the last item in an array or a default value.
Usage
last([1, 2, 3, 4]);// 4
last([], 0);// 0
Installation
npx atmx add helper last
Copy and paste the following method into @/utils/helpers/undefined.ts
:
/*** Get the last item in an array or a default value.** @example* last([1, 2, 3, 4])* // 4** last([], 0)* // 0*/export function last<T>(array: readonly T[]): T | undefined;
export function last<T, U>(array: readonly T[], defaultValue: U): T | U;
export function last(array: readonly unknown[], defaultValue?: unknown) {return array?.length > 0 ? array[array.length - 1] : defaultValue;}