Skip to main content

latest
Works with
It is unknown whether this package works with Browsers, Deno, Node.js, Cloudflare Workers, Bun
It is unknown whether this package works with Browsers
It is unknown whether this package works with Deno
It is unknown whether this package works with Node.js
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Bun
JSR Score70%
Downloads1/wk
Published2 years ago (0.215.0)
function aggregateGroups
aggregateGroups<T, A>(
record: Readonly<Record<string, ReadonlyArray<T>>>,
aggregator: (
current: T,
key: string,
first: boolean,
accumulator?: A
) => A
): Record<string, A>

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

Examples

Example 1

import { aggregateGroups } from "@std/collections/aggregate_groups";
import { assertEquals } from "@std/assert/assert_equals";

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",
});

Type Parameters

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

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

Parameters

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

Return Type

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/collections

Import symbol

import { aggregateGroups } from "@std/collections/aggregate_groups";
or

Import directly with a jsr specifier

import { aggregateGroups } from "jsr:@std/collections/aggregate_groups";