Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/collections/mod.ts>groupBy

Deno standard library
Go to Latest
function groupBy
Deprecated
Deprecated

(will be removed in 0.211.0) Use Object.groupBy instead.

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.

import { groupBy } from "https://deno.land/std@0.208.0/collections/mod.ts";

Examples

Example 1

import { groupBy } from "https://deno.land/std@0.208.0/collections/group_by.ts";
import { assertEquals } from "https://deno.land/std@0.208.0/assert/assert_equals.ts";

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

assertEquals(
  peopleByFirstLetter,
  {
    "A": [{ name: "Anna" }, { name: "Arnold" }],
    "K": [{ name: "Kim" }],
  },
);

Type Parameters

T
K extends PropertyKey

Parameters

iterable: Iterable<T>
selector: (element: T, index: number) => K

Returns

Partial<Record<K, T[]>>