@jiminp/tooltool
    Preparing search index...

    Function multimapAdd

    • Adds a value v to the array associated with key k inside a Map-backed multimap.

      If the key does not yet exist in the map, a new array is created, added to the map, and then the value is appended. If the key already exists, the value is appended to the existing array.

      Type Parameters

      • K

        The type of keys in the map.

      • V

        The type of values stored in the multimap.

      Parameters

      • m: Map<K, V[]>

        A Map where each key maps to an array of values.

      • k: K

        The key under which the value should be inserted.

      • v: V

        The value to add to the key's array.

      Returns V[]

      The array associated with key k after insertion.

      const mm = new Map<string, number[]>();
      multimapAdd(mm, "a", 1);
      multimapAdd(mm, "a", 2);
      console.log(mm.get("a"));
      // → [1, 2]
      const mm = new Map<number, boolean[]>();
      const arr = multimapAdd(mm, 42, true);
      console.log(arr);
      // → [true]
      console.log(mm.get(42) === arr);
      // → true (same reference)
    • Adds a value v to the array associated with key k inside a Record-backed multimap.

      If the key does not yet exist in the record, a new array is created, added to the record, and then the value is appended. If the key already exists, the value is appended to the existing array.

      Type Parameters

      • K extends PropertyKey

        The type of keys in the record (must extend PropertyKey).

      • V

        The type of values stored in the multimap.

      Parameters

      • m: Record<K, V[]>

        A Record where each key maps to an array of values.

      • k: K

        The key under which the value should be inserted.

      • v: V

        The value to add to the key's array.

      Returns V[]

      The array associated with key k after insertion.

      const mm: Record<string, string[]> = {};
      multimapAdd(mm, "fruits", "apple");
      multimapAdd(mm, "fruits", "banana");
      multimapAdd(mm, "vegetables", "carrot");

      console.log(mm["fruits"]);
      // → ["apple", "banana"]
      console.log(mm["vegetables"]);
      // → ["carrot"]
      const mm: Record<number, boolean[]> = {};
      const arr = multimapAdd(mm, 42, true);
      console.log(arr);
      // → [true]
      console.log(mm[42] === arr);
      // → true (same reference)