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

    Function average

    • Computes the arithmetic mean of the numeric values produced for each element in the sequence.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalselector: Selector<TElement, number>

        Optional projection that extracts the numeric value for each element. Defaults to the element itself.

      Returns number

      The arithmetic mean of the selected values.

      Thrown when source is empty.

      Provide a selector when the elements are not already numeric. All values are enumerated exactly once.

      const numbers = [1, 2, 3, 4, 5];
      const avg = average(numbers);
      console.log(avg); // 3

      const people = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      { name: 'Charlie', age: 35 },
      ];
      const avgAge = average(people, p => p.age);
      console.log(avgAge); // 30