Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/fathym_common/src/src.deps.ts>reduceGroups

The Fathym Reference Architecture provides the common foundation for applications built in Typescript.
Go to Latest
function reduceGroups
import { reduceGroups } from "https://deno.land/x/fathym_common@v0.0.158/src/src.deps.ts";

Applies the given reducer to each group in the given grouping, returning the results together with the respective group keys.

Examples

Example 1

import { reduceGroups } from "https://deno.land/std@0.224.0/collections/reduce_groups.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";

const votes = {
  "Woody": [2, 3, 1, 4],
  "Buzz": [5, 9],
};

const totalVotes = reduceGroups(votes, (sum, it) => sum + it, 0);

assertEquals(totalVotes, {
  "Woody": 10,
  "Buzz": 14,
});

Type Parameters

T

input type of an item in a group in the given grouping.

A

type of the accumulator value, which will match the returned record's values.

Parameters

record: Readonly<Record<string, ReadonlyArray<T>>>
reducer: (accumulator: A, current: T) => A
initialValue: A

Returns

Record<string, A>