Skip to main content

Function: table()

table<T>(items, fields): string

Function to create a table from an array of objects.

Type Parameters

T extends Record<string, unknown> = Record<string, unknown>

The type of the objects in the array. Should be inferred from the items parameter.

Parameters

items: T[]

The array of objects to create the table from.

fields: TableField<T>[]

The fields to include in the table. See TableField.

Returns

string

Markdown table.

Example

const items = [{name: 'Alice', age: 30}, {name: 'Bob', age: 40}];
console.log(table(items, ['name', 'age']));
// Prints:
//
// | name | age |
// | ---- | --- |
// | Alice | 30 |
// | Bob | 40 |

Defined in

lib/markdown.ts:316