How can i find median values from array in javascript
this is my array
var data = [
{ values: 4 },
{ values: 4 },
{ values: 4 },
{ values: 5 },
{ values: 2 },
{ values: 6 },
{ values: 6 },
{ values: 5 }
];
and i have tried this code
function findMedian(m) {
var middle = m.length - 1 / 2;
if (m.length - 1 % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
But it's return NaN
value
My calculation formula is
Find the median of the data. Place the number of data in ascending order. Then, mark the place whose value we take into account when calculating the median.