source: node_modules/es-toolkit/dist/map/reduce.d.ts@ ba17441

Last change on this file since ba17441 was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/**
2 * Reduces a Map to a single value by iterating through its entries and applying a callback function.
3 *
4 * This function iterates through all entries of the Map and applies the callback function to each entry,
5 * accumulating the result. If an initial value is provided, it is used as the starting accumulator value.
6 * If no initial value is provided and the Map is empty, a TypeError is thrown.
7 *
8 * @template K - The type of keys in the Map.
9 * @template V - The type of values in the Map.
10 * @param {Map<K, V>} map - The Map to reduce.
11 * @param {(accumulator: V, value: V, key: K, map: Map<K, V>) => V} callback - A function that processes each entry and updates the accumulator.
12 * @param {V} [initialValue] - The initial value for the accumulator. If not provided, the first value in the Map is used.
13 * @returns {V} The final accumulated value.
14 * @throws {TypeError} If the Map is empty and no initial value is provided.
15 *
16 * @example
17 * const map = new Map([
18 * ['a', 1],
19 * ['b', 2],
20 * ['c', 3]
21 * ]);
22 * const result = reduce(map, (acc, value) => acc + value, 0);
23 * // result will be: 6
24 *
25 * @example
26 * const map = new Map([
27 * ['a', 10],
28 * ['b', 20]
29 * ]);
30 * const result = reduce(map, (acc, value) => acc + value);
31 * // result will be: 30 (starts with first value 10)
32 */
33declare function reduce<K, V, A = V>(map: Map<K, V>, callback: (accumulator: A, value: V, key: K, map: Map<K, V>) => A, initialValue?: A): A;
34
35export { reduce };
Note: See TracBrowser for help on using the repository browser.