Convert Array to String
convertArrayToStringβ
Joins elements of an array of primitive values or array of objects into a single string using a custom separator.
Importβ
import { convertArrayToString } from 'nhb-toolbox';
Function Signaturesβ
- Array of Primitives
- Array of Objects
function convertArrayToString<T extends Primitive>(
array: T[] | undefined,
options?: {
separator?: string;
}
): string;
function convertArrayToString<T extends GenericObject>(
array: T[] | undefined,
options: {
target: NestedPrimitiveKey<T>;
separator?: string;
}
): string;
Usage Examplesβ
- Primitive Default
- Primitive Custom
- Object Nested Key
- Custom Separator
- Empty Array
convertArrayToString(['a', 'b', 'c']);
// "a, b, c"
convertArrayToString([1, 2, 3], { separator: ' - ' });
// "1 - 2 - 3"
convertArrayToString(
[{ user: { name: 'Alice' } }, { user: { name: 'Bob' } }],
{ target: 'user.name' }
);
// "Alice, Bob"
convertArrayToString(['JS', 'TS', 'React'], { separator: ' | ' });
// "JS | TS | React"
convertArrayToString([], { separator: ';' });
// ""
API Referenceβ
Parametersβ
Name | Type | Description |
---|---|---|
array | T[] | Array of primitives or objects to convert to string |
options | object | Options for separator and target key (if object array) |
options.separator | string | Optional separator for joining values (default: ", " ) |
options.target | string | Required if array contains objects. Dot-accessible key to extract primitive values only |
Returnsβ
A string formed by joining the values (primitive or extracted) with the given separator.
Key Featuresβ
- β Supports Primitives and Objects: Handles both types cleanly with overloads.
- π§© Nested Key Extraction: Extracts deep object properties using dot notation like
"user.name"
. - π§Ό Handles Empty Inputs: Returns empty string if array is
undefined
or empty. - π οΈ Customizable Separator: Use any string to separate values (e.g.,
" - "
,"|"
, etc.).
Recommended Use Casesβ
- Formatting tag or category lists for display.
- Preparing data for tooltips, labels, logs, or CSV-like formats.
- Joining object property values for human-readable summaries.
Notesβ
- This function always checks for array validity before processing.
- For object arrays, use the
target
option to specify the key to extract (e.g.,"user.name"
). - TypeScript enforces that the
target
path must resolve to a primitive value (likestring
,number
,boolean
,null
, orundefined
). In JavaScript, you may pass any path, but non-primitive values (e.g., objects, arrays) will result in[object Object]
during stringification. - If you need to format, localize, or transform values before joining, consider mapping the array beforehand:
convertArrayToString(users.map(u => u.name.trim()), { separator: ' β’ ' });
Limitationsβ
Requires a valid array. If not, it returns an empty string.
Warning
- Does not auto-serialize objects. You must provide a
target
key when working with object arrays. - Only primitive values are supported correctly for both primitive arrays and object-based arrays.
- Nested
target
keys must resolve to primitive values inside objects. TypeScript will error if you reference a non-primitive type. - There is no built-in formatting or filtering β the function joins raw values directly.
Aliasesβ
joinArrayElements
β named export alias forconvertArrayToString
Conclusionβ
convertArrayToString
makes joining arrays clean, type-safe, and flexibleβperfect for tags, logs, tooltips, and more. Just supply your array and desired separator!