deno.land / std@0.166.0 / collections / reduce_groups.ts

reduce_groups.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { mapValues } from "./map_values.ts";
/** * Applies the given reducer to each group in the given Grouping, returning the results together with the respective group keys * * ```ts * import { reduceGroups } from "https://deno.land/std@$STD_VERSION/collections/reduce_groups.ts" * import { assertEquals } from "https://deno.land/std@$STD_VERSION/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, * }) * ``` */export function reduceGroups<T, A>( record: Readonly<Record<string, Array<T>>>, reducer: (accumulator: A, current: T) => A, initialValue: A,): Record<string, A> { return mapValues(record, (it) => it.reduce(reducer, initialValue));}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago