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

    Function arrGroupBy

    • Function

      The arrGroupBy() method groups array elements by the result of a callback function, returning an object where keys are group identifiers and values are arrays of grouped elements.

      Type Parameters

      • T

        Identifies the base type of array elements

      Parameters

      • theArray: ArrayLike<T>

        The array or array-like object to group

      • callbackFn: ArrGroupByCallbackFn<T>

        Function that determines the group key for each element

      • OptionalthisArg: any

        The value to use as 'this' when executing callbackFn

      Returns Record<string | number | symbol, T[]>

      An object with group keys as properties and arrays of grouped elements as values

      0.14.0

      const numbers = [1, 2, 3, 4, 5, 6];
      const grouped = arrGroupBy(numbers, (n) => n % 2 === 0 ? "even" : "odd");
      // { odd: [1, 3, 5], even: [2, 4, 6] }

      const people = [
      { name: "Alice", age: 30 },
      { name: "Bob", age: 25 },
      { name: "Charlie", age: 30 }
      ];
      const byAge = arrGroupBy(people, (p) => p.age);
      // { "25": [{ name: "Bob", age: 25 }], "30": [{ name: "Alice", age: 30 }, { name: "Charlie", age: 30 }] }