@nevware21/ts-utils
    Preparing search index...

    Type Alias ArrGroupByCallbackFn<T>

    ArrGroupByCallbackFn: (
        value: T,
        index: number,
        array: ArrayLike<T>,
    ) => string | number | symbol

    Callback function type used by arrGroupBy to derive a group key for each element. The function is called once per element and must return a string, number, or symbol that identifies which group the element belongs to. Elements that map to the same key are collected into the same array in the result object.

    Type Parameters

    • T

      Identifies the base type of array elements

    Type Declaration

      • (value: T, index: number, array: ArrayLike<T>): string | number | symbol
      • Parameters

        • value: T

          The current element of the array being processed.

        • index: number

          The zero-based index of the current element in the array.

        • array: ArrayLike<T>

          The array (or array-like object) that arrGroupBy was called on.

        Returns string | number | symbol

        A string, number, or symbol that identifies the group for the current element.

    0.14.0

    // Group numbers as "even" or "odd"
    const parity: ArrGroupByCallbackFn<number> = (n) => n % 2 === 0 ? "even" : "odd";

    arrGroupBy([1, 2, 3, 4, 5], parity);
    // { odd: [1, 3, 5], even: [2, 4] }
    // Group objects by a property value
    interface Person { name: string; dept: string; }

    const byDept: ArrGroupByCallbackFn<Person> = (p) => p.dept;

    const people: Person[] = [
    { name: "Alice", dept: "eng" },
    { name: "Bob", dept: "hr" },
    { name: "Carol", dept: "eng" }
    ];

    arrGroupBy(people, byDept);
    // {
    // eng: [{ name: "Alice", dept: "eng" }, { name: "Carol", dept: "eng" }],
    // hr: [{ name: "Bob", dept: "hr" }]
    // }
    // Use the element index to create fixed-size buckets
    const bucket: ArrGroupByCallbackFn<string> = (_v, idx) => idx % 3;

    arrGroupBy(["a", "b", "c", "d", "e"], bucket);
    // { 0: ["a", "d"], 1: ["b", "e"], 2: ["c"] }