• Groups an array of objects by a composite key derived from specified object keys and returns a new object with these keys omitted.

    The function works by first creating a unique key for each object in the array by concatenating the string representations of the specified keys' values. Then, it groups the objects based on these composite keys. Finally, it omits the specified keys from each object in the grouped result.

    The function is generic and can be used with any object type, provided the keys specified are part of the object and do not include methods.

    Type Parameters

    • T extends Record<string, unknown>

      The type of objects in the input array. Should be a record type.

    • TKeys extends readonly (keyof ExcludeMethods<T>)[]

      The type of the array of keys to group by and omit. Must be a readonly array of keys of T.

    Parameters

    • data: T[]

      The array of objects to group and modify.

    • omitKeys: TKeys

      An array of keys to be used for creating the group key and to be omitted in the result.

    Returns Record<string, Omit<T, TKeys[number]>[]>

    An object where each key is a composite key derived from the omitKeys values and each value is an array of objects with the specified keys omitted.

    Example

    // Grouping by 'name' and omitting 'name' key in the result
    groupByOmit([{ id: 1, name: 'Alice', age: 30 }, { id: 2, name: 'Bob', age: 40 }, { id: 3, name: 'Alice', age: 35 }], ['name'])
    // Returns: { 'Alice': [{ id: 1, age: 30 }, { id: 3, age: 35 }], 'Bob': [{ id: 2, age: 40 }] }

Generated using TypeDoc