Function arrTableToObjects

  • Converts a 2D array representing a table into an array of objects.

    Type Parameters

    • T

      The type of the elements in the rows.

    Parameters

    • rows: T[][]

      The 2D array representing the table.

    • Optional headers: string[]

      The headers to use as keys for the objects. If not provided, the first row of the table is used as headers.

    • Optional ignoreKeys: Set<string>

    Returns Record<string, T>[]

    An array of objects, where each object represents a row in the table.

    Throws

    Throws an error if the headers are not provided and the table is empty or only contains one row.

    Example

    const table = [
    ['Name', 'Age', 'Country'],
    ['John', 25, 'USA'],
    ['Jane', 30, 'Canada'],
    ];
    const headers = ['Name', 'Age', 'Country'];
    arrTableToObjects(table, headers) //=> [
    // { Name: 'John', Age: 25, Country: 'USA' },
    // { Name: 'Jane', Age: 30, Country: 'Canada' },
    // ]

Generated using TypeDoc