@technobuddha > library > Array > Operations
Function: collapse()
ts
function collapse<T>(...args: Collapsible<T>[]): T[];Defined in: collapse.ts:82
Collapses an array of values into a flat array with null and undefined elements removed.
Each argument can be:
Tnullundefined- a function returning
Tornullorundefinedor an array - An iterable returning
Tornullorundefined
The function flattens all arguments, filters out null, and undefined values, and returns the resulting array.
Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
T extends CollapsiblePrimitive | string | The primitive type that can be collapsed. |
Parameters
| Parameter | Type | Description |
|---|---|---|
...args | Collapsible<T>[] | The values to collapse, which may be T, generators, iterables, or functions. |
Returns
T[]
An array of T, with all null, and undefined values removed.
Example
typescript
collapse(
"hello",
["world", null, "foo"],
function* () { yield "bar"; yield undefined; yield ["baz"]; },
() => "qux",
null,
undefined
);
// Returns: ["hello", "world", "foo", "bar", "baz", "qux"]