For an array of numbers, for example. [1,6,3,9,28,...]
What the code does:
- Sort the numbers so that they are in order by value.
- Find the average index in the array. If the length of the array is even, the median is the average of two numbers on either side of the index.
- For arrays of odd length it is easy to cut the average. But for arrays of even length, this is not so. So, you check to see if your array is odd or even long-lived by finding out whether the length of the array will divide by two numbers into an integer or not. If it is an integer, it means that the length of the array is even, and you need to calculate the average of two numbers on either side of the median.
In your question, your array can be flattened as follows:
var myArray = data.map(function(d){ return d.values; });
And to get the median, use the function above:
var myMedian = median(myArray);
source share