std/collections/mod.ts

Deno standard library
Go to Latest
function reduceGroups
import { reduceGroups } from "https://deno.land/std@0.145.0/collections/mod.ts?s=reduceGroups";

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

import { reduceGroups } from "https://deno.land/std@0.145.0/collections/reduce_groups.ts"
import { assertEquals } from "https://deno.land/std@0.145.0/testing/asserts.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,
})

Parameters

record: Readonly<Record<string, Array<T>>>[src]
reducer: (accumulator: A, current: T) => A[src]
initialValue: A[src]

Returns

Record<string, A>[src]