@mirei/ts-collections
    Preparing search index...

    Function sum

    • Computes the sum of the numeric values produced for each element.

      Parameters

      • source: Iterable<number>

        The source iterable.

      Returns number

      The sum of the projected values.

      Thrown when source is empty.

      source is enumerated exactly once. Supply selector when elements are not already numeric.

      const numbers = [1, 2, 3, 4, 5];
      const total = sum(numbers);
      console.log(total); // 15

      const people = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      ];
      const totalAge = sum(people, p => p.age);
      console.log(totalAge); // 55
    • Computes the sum of the numeric values produced for each element.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • selector: Selector<TElement, number>

        Optional projection that extracts the numeric value. Defaults to interpreting the element itself as a number.

      Returns number

      The sum of the projected values.

      Thrown when source is empty.

      source is enumerated exactly once. Supply selector when elements are not already numeric.

      const numbers = [1, 2, 3, 4, 5];
      const total = sum(numbers);
      console.log(total); // 15

      const people = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      ];
      const totalAge = sum(people, p => p.age);
      console.log(totalAge); // 55