Skip to content

safeAppend

safeAppend<T>(arr, item): T[]

Defined in: array/safe-append.ts:19

Safely append an item which may not be defined to an array. This function returns a copy of the source array and does not mutate the original.

Type Parameters

T

T

Parameters

arr

T[]

The original array

item

The item to append, which may be undefined or null

T | null | undefined

Returns

T[]

A new array with the item appended if it is defined; otherwise, the original array

Example

safeAppend([1, 2, 3], 4); // [1, 2, 3, 4]
safeAppend([1, 2, 3], null); // [1, 2, 3]
safeAppend([1, 2, 3], undefined); // [1, 2, 3]