Partitions an array into a Map-based multimap using a key extractor function.
Map
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.
The type of elements in the input array.
The type of keys in the resulting map.
The array of elements to partition.
A function that produces a key from an element.
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] Copy
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"] Copy
const words = ["apple", "banana", "avocado", "cherry"];const byFirstChar = partitionToMultimap(words, (w) => w[0]);console.log(byFirstChar.get("a"));// → ["apple", "avocado"]
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
Mapwhere each key holds an array of all elements that produced that key.