Function sumOf

  • Applies the given selector to all elements in the given collection and calculates the sum of the results.

    Type Parameters

    • T

      The type of the array elements.

    Parameters

    • array: Iterable<T, any, any>

      The array to calculate the sum of.

    • selector: ((el: T) => number)

      The selector function to get the value to sum.

        • (el): number
        • Parameters

          • el: T

          Returns number

    Returns number

    The sum of all elements in the collection.

    import { sumOf } from "@std/collections/sum-of";
    import { assertEquals } from "@std/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);