sumOf
import { sumOf } from "jsr:@ende/collections";function
function sumOf<T>(array: Iterable<T>, selector: (el: T, index: number) => number): numberApplies the given selector to all elements in the given collection and calculates the sum of the results.
array: Iterable<T>The array to calculate the sum of.
selector: (el: T, index: number) => numberThe selector function to get the value to sum. The function receives the element and its index.
Returns
The sum of all elements in the collection.
Example: Basic usage
import { sumOf } from "@ende/collections/sum-of";
import { assertEquals } from "@ende/assert";
const people = [
{ name: "Anna", age: 34 },
{ name: "Kim", age: 42 },
{ name: "John", age: 23 },
];
const totalAge = sumOf(people, (person) => person.age);
assertEquals(totalAge, 99);
Example: Using the index parameter
import { sumOf } from "@ende/collections/sum-of";
import { assertEquals } from "@ende/assert";
const array = [1, 2, 3];
const result = sumOf(array, (_, index) => index);
assertEquals(result, 3);