|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
817 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Calculates the sum of an array of numbers when applying
|
|---|
| 3 | * the `getValue` function to each element.
|
|---|
| 4 | *
|
|---|
| 5 | * If the array is empty, this function returns `0`.
|
|---|
| 6 | *
|
|---|
| 7 | * @template T - The type of elements in the array.
|
|---|
| 8 | * @param {readonly T[]} items - An array to calculate the sum.
|
|---|
| 9 | * @param {(element: T, index: number) => number} getValue - A function that selects a numeric value from each element.
|
|---|
| 10 | * It receives the element and its zero‑based index in the array.
|
|---|
| 11 | * @returns {number} The sum of all the numbers as determined by the `getValue` function.
|
|---|
| 12 | *
|
|---|
| 13 | * @example
|
|---|
| 14 | * sumBy([{ a: 1 }, { a: 2 }, { a: 3 }], (x, i) => x.a * i); // Returns: 8
|
|---|
| 15 | * sumBy([], () => 1); // Returns: 0
|
|---|
| 16 | */
|
|---|
| 17 | declare function sumBy<T>(items: readonly T[], getValue: (element: T, index: number) => number): number;
|
|---|
| 18 |
|
|---|
| 19 | export { sumBy };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.