Function fixListCreator

  • Maps a two-dimensional array of values to an array of objects.

    For each inner array in itemArr, this function creates an object with:

    • A unique identifier under the property id (a 3-character alphanumeric string generated by generate).
    • Additional properties whose keys are defined by the keys array and whose values are taken from the inner array at corresponding indices.

    Important: The length of each inner array in itemArr should match the length of the keys array.

    Type Parameters

    • R

      The type describing additional properties for each generated object.

    Parameters

    • itemArr: FixListCreatorType<R>["id" | keyof R][][]

      A two-dimensional array where each inner array contains values to be mapped.

    • keys: string[]

      An array of strings representing the keys to assign values from each inner array.

    Returns ({ id: string } & R)[]

    An array of objects, each with a unique id and properties as defined by keys.

    export const PERSONS = generator<{firstName: string, lastName: string, age: number}>(
    [
    ['john', 'Doe', 30],
    ['Jane', 'Smith', 25],
    ],
    ['firstName', 'lastName', 'age'],
    );

    // Example output:
    // console.log(PERSONS)
    // [
    // { id: 'a1B', firstName: 'John', lastName: 'Doe', age: 30 },
    // { id: 'c3D', firstName: 'Jane', lastName: 'Smith', age: 25 }
    // ]
    const result = generator(data, keys);