an object that maps group keys to arrays of elements/values
Group elements by their string length.
import { groupBy } from "@trashpanda001/helpers/array"
groupBy(["one", "two", "three", "four", "five"], (x) => x.length)
// { "3": ["one", "two"], "4": ["four", "five"], "5": ["three"] }
groupBy(["one", "two", "three", "four", "five"], (x) => x.length, (x) => x.toUpperCase())
// { "3": ["ONE", "TWO"], "4": ["FOUR", "FIVE"], "5": ["THREE"] }
Splits an array into groups based on
keyFn
.The result is an object where each key is given by
keyFn
and each value is an array of elements given byvalueFn
. The order of elements within each list is preserved from the original array.