@jiminp/tooltool
    Preparing search index...

    Function partitionToMultimap

    • Partitions an array into a Map-based multimap using a key extractor function.

      Each element in the array is mapped to a key via the provided function. The result is a Map where each key holds an array of all elements that produced that key.

      Type Parameters

      • T

        The type of elements in the input array.

      • U

        The type of keys in the resulting map.

      Parameters

      • arr: T[]

        The array of elements to partition.

      • key: (t: T) => U

        A function that produces a key from an element.

      Returns Map<U, T[]>

      A Map grouping elements by their extracted keys.

      const numbers = [1, 2, 3, 4, 5];
      const parity = partitionToMultimap(numbers, (n) => n % 2 === 0 ? "even" : "odd");

      console.log(parity.get("odd"));
      // → [1, 3, 5]
      console.log(parity.get("even"));
      // → [2, 4]
      const words = ["apple", "banana", "avocado", "cherry"];
      const byFirstChar = partitionToMultimap(words, (w) => w[0]);

      console.log(byFirstChar.get("a"));
      // → ["apple", "avocado"]