Skip to main content
Go to Latest
function groupBy
import { groupBy } from "https://deno.land/std@0.102.0/collections/group_by.ts";

Applies the given selector to each element in the given array, returning a Record containing the results as keys and all values that produced that key as values.

Example:

const people = [
    { name: 'Anna' },
    { name: 'Arnold' },
    { name: 'Kim' },
]
const peopleByFirstLetter = groupBy(people, it => it.name.charAt(0))

console.assert(peopleByFirstLetter === {
    'A': [ { name: 'Anna' }, { name: 'Arnold' } ],
    'K': [ { name: 'Kim' } ],
})

Parameters

array: Array<T>
selector: Selector<T, string>