ensureArray
Wraps a value in an array if it is not already one.
If the value is already an array, it is returned as-is.
If the value is null or undefined, returns an empty array.
When a depth is specified, the resulting array is flattened
to that depth (like Array.prototype.flat(depth)).
Available since v2.0.0
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | T | readonly T[] | null | undefined | The value to ensure is an array |
Returns
Section titled “Returns”T[] — The value wrapped in an array, or the value itself if already an array
Examples
Section titled “Examples”Wrap a single value
Section titled “Wrap a single value”Wraps a non-array value in an array.
Pass through an existing array
Section titled “Pass through an existing array”Returns the array as-is if already an array.
Handle null and undefined
Section titled “Handle null and undefined”Returns an empty array for null or undefined values.
Flatten nested arrays with depth
Section titled “Flatten nested arrays with depth”Flattens the resulting array to a given depth, like Array.prototype.flat().
