std/collections/aggregate_groups.ts

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

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

import { aggregateGroups } from "https://deno.land/std@0.147.0/collections/aggregate_groups.ts";
import { assertEquals } from "https://deno.land/std@0.147.0/testing/asserts.ts";

const foodProperties = {
    'Curry': [ 'spicy', 'vegan' ],
    'Omelette': [ 'creamy', 'vegetarian' ],
}
const descriptions = aggregateGroups(foodProperties,
    (current, key, first, acc) => {
        if (first)
            return `${key} is ${current}`

        return `${acc} and ${current}`
    },
)

assertEquals(descriptions, {
    'Curry': 'Curry is spicy and vegan',
    'Omelette': 'Omelette is creamy and vegetarian',
})

Parameters

record: Readonly<Record<string, Array<T>>>[src]
aggregator: (
current: T,
key: string,
first: boolean,
accumulator?: A,
) => A
[src]

Returns

Record<string, A>[src]