matician - v2.0.0
    Preparing search index...

    Function varianceBy

    • Calculates the sample variance of a specified numeric property in an array of objects. Uses the formula: variance = Σ((x - mean)²) / (n - 1)

      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 used.

      Returns number

      The sample variance, or NaN if fewer than 2 valid numbers exist.

      const data = [{ a: 2 }, { a: 4 }, { a: 4 }, { a: 4 }, { a: 5 }, { a: 5 }, { a: 7 }, { a: 9 }];
      varianceBy(data, 'a'); // Returns 4