es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
25 lines (24 loc) • 854 B
JavaScript
//#region src/bigint/sumBy.ts
/**
* Calculates the sum of an array by mapping each element to a bigint.
*
* This function takes an array and a function that maps each element to a bigint,
* and returns the sum of the mapped values. An empty array returns `0n`.
*
* @template T - The type of elements in the array.
* @param items - An array of elements to be summed.
* @param getValue - A function that maps an element to the bigint to add.
* @returns The sum of the mapped bigints. Returns `0n` for an empty array.
*
* @example
* const items = [{ balance: 10n }, { balance: 20n }, { balance: 30n }];
* const result = sumBy(items, item => item.balance);
* // result will be 60n
*/
function sumBy(items, getValue) {
let result = 0n;
for (let i = 0; i < items.length; i++) result += getValue(items[i], i);
return result;
}
//#endregion
export { sumBy };