matician - v2.0.0
    Preparing search index...

    Function meanBy

    • Calculates the mean (average) of a specified numeric property in an array of objects.

      Type Parameters

      • T extends Record<string, any>

        The type of objects in the array.

      Parameters

      • items: T[]

        The array of objects.

      • key: keyof T

        The key whose numeric values will be averaged.

      Returns number

      The mean value for the specified key, or 0 if no valid numbers are found.

      const data = [{ a: 2 }, { a: 4 }, { a: 6 }];
      meanBy(data, 'a'); // Returns 4