medianBy es-toolkit
Calculates the median of an array by applying the getValue function to each element.
Usage
medianBy(items, getValue)
Use medianBy when you want to find the median of the results of applying a function to each element of an array. It's useful for calculating the median of a specific property from an array of objects or for finding the median after transforming each element. For an array with an odd number of elements, it returns the exact middle value, and for an array with an even number of elements, it returns the average of the two middle values. If given an empty array, it returns NaN.
Parameters
An array to calculate the median.
A function that selects a numeric value from each element.
Returns
Returns the median of all values as determined by the getValue function. Returns NaN if the array is empty.
Source: es-toolkit
Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.