ts-iterable-functions
    Preparing search index...

    Function _groupBy

    • Groups elements from the source iterable by key and returns lazy grouped iterables for each bucket.

      Type Parameters

      • T

        Element type produced by the source iterable.

      • TKey

        Key type generated by the key selector.

      Parameters

      • src: Iterable<T>

        Source iterable enumerated to populate the lookup.

      • keySelector: IndexedSelector<T, TKey>

        Selector invoked with each element and index to produce a key.

      • OptionalmapFactory: MapFactory<TKey>

        Optional factory used to construct the internal map for lookups.

      Returns Iterable<GroupedIterable<TKey, T>>

      A deferred iterable emitting grouped iterables keyed by the selector output.

      const groups = [..._groupBy(
      ["ant", "bat", "apple"],
      (word) => word[0]
      )].map((group) => ({ key: group.key, values: [...group] }));
      console.log(groups);
      // [
      // { key: "a", values: ["ant", "apple"] },
      // { key: "b", values: ["bat"] }
      // ]

      or using the curried version:

      const groups = pipeInto(
      [
      { category: "fruit", item: "apple" },
      { category: "fruit", item: "banana" },
      { category: "veg", item: "carrot" }
      ],
      groupBy((entry) => entry.category)
      );
      const shaped = [...groups].map((group) => ({
      key: group.key,
      items: [...group].map((entry) => entry.item)
      }));
      console.log(shaped);
      // [
      // { key: "fruit", items: ["apple", "banana"] },
      // { key: "veg", items: ["carrot"] }
      // ]