• Groups an array of objects by a given property and returns an array of objects with a group key and an array of grouped objects as its items value.

    Remarks

    The resulting objects in the items array will only contain properties that are listed in the includedProps array. If destruct is set to true, the original objects in the items array will be destructed and only the filtered properties will remain.

    Example

    Example usage, grouping by country and age, destruct name property

    const input = [
    { name: 'John', age: 30, country: 'USA' },
    { name: 'Alice', age: 25, country: 'Canada' },
    { name: 'Bob', age: 35, country: 'USA' },
    { name: 'Eve', age: 28, country: 'Canada' }
    ];

    groupedByMany(input, 'country.age', ['name'], true)
    // =>
    // { ok: true, value:
    // [
    // { group: 'USA.30', items: [{ age: 30 }] },
    // { group: 'Canada.25', items: [{ age: 25 }] },
    // { group: 'USA.35', items: [{ age: 35 }] },
    // { group: 'Canada.28', items: [{ age: 28 }] }
    // ]
    // }

    Type Parameters

    • T

      The type of the objects in the input array.

    Parameters

    • arr: T[]

      The array of objects to group.

    • key: string
    • Optional propertiesToInclude: (keyof T)[]

    Returns Result<GroupedObject<T>[], Error>

    A Result object containing an array of objects representing the groups and their items, or an error if the input is not valid.

Generated using TypeDoc v0.24.7