Function groupByProperty

  • Groups an array of objects by the specified property.

    Returns

    An object where each key is the stringified value of the specified property, and each value is an array of objects from items that share that property value.

    Example

    const data = [
    { index: '0x1600', value: '0x60400010' },
    { index: '0x1600', value: '0x60600008' },
    { index: '0x1601', value: '0x60fe0120' }
    ];

    const grouped = groupByProperty(data, 'index');
    // {
    // "0x1600": [
    // { index: "0x1600", value: "0x60400010" },
    // { index: "0x1600", value: "0x60600008" }
    // ],
    // "0x1601": [
    // { index: "0x1601", value: "0x60fe0120" }
    // ]
    // }

    Type Parameters

    • T

      The type of objects in the input array.

    • K extends string | number | symbol

      The key of the property in T to group by.

    Parameters

    • items: T[]

      The array of objects to group.

    • property: K

      The property name to group by. The property must be a key of T.

    Returns Record<string, T[]>

Generated using TypeDoc