In your array, only one user (Bugs Bunny) is older than the median age, so the average would just be his age, 32. But here is the code to do it for any set of users, given the data structure in your question.
var data = {
users: [
{ first_name: "Mikey", last_name: "Mouse", age: 24 },
{ first_name: "Donald", lastName: "Duck", age: 29 },
{ first_name: "Woody", lastName: "Woodpecker", age: null },
{ first_name: "Bugs", lastName: "Bunny", age: 32 }
]
};
// first calculate the median
// see https://stackoverflow.com/questions/25305640/find-median-values-from-array-in-javascript-8-values-or-9-values/39639518#39639518
function median(arr){
arr = arr.sort(function(a, b){ return a - b; });
var i = arr.length / 2;
return i % 1 == 0 ? (arr[i - 1] + arr[i]) / 2 : arr[Math.floor(i)];
}
var med = median(data.users.map(function(d){ return d.age; }).filter(function(d){ return typeof d == "number"; }));
// now calculate the average of those users whose age is above the median
var users_above_median = data.users.filter(function(d){ return d.age > med; });
function average(arr){
return sum(arr) / arr.length;
}
function sum(arr){
return arr.reduce(function(d, i){ return i + d; });
}
var avg = average(users_above_median.map(function(d){ return d.age; }));
console.log(avg);