I am writing a generic function to iterate a jSon file as below:
function test(data, type) {
var k = 0;
for (var i in data) {
k += isNaN(data[i].type) ? 0 : data[i].type;
}
return (k / data.length);
}
where type
is a dynamic value - it can be "steps", "distance", "floors" etc
What I want to do is that every time when I call the function, it should find what type
is and find the average for that type. Is there a way to do that?
jSon file is below:
[
{
"ActivitySummaryKey": 23323,
"activitycalories": 768,
"caloriesBMR": 1052,
"caloriesOut": 1622,
"distances": 4.14,
"elevation": 24,
"fairlyActiveminutes": 62,
"floors": 8,
"lightlyActiveMinutes": 125,
"marginalCalories": 476,
"sedentaryMinutes": 258,
"steps": 5547,
"veryActiveMinutes": 11,
"createddate": "5/27/2014 12:00:00 AM"
}
]