So I'm almost embarrassed to be asking this, but alas, I've reached my head scratching threshold... I have this data structure which is a nested array of objects representing a span of 3 subsequent days. I need to loop through the entire array (what I'm working on is much larger than this..) and obtain the average of 'x' for those three days and I also need to get the average for 'y' for this same time period.
var myArray = [
{
day: 1,
values: [
{
x: 8
}, {
y: 10
}
]
}, {
day: 2,
values: [
{
x: 9
}, {
y: 15
}
]
}, {
day: 3,
values: [
{
x: 10
}, {
y: 16
}
]
}
];
I have this nested loop...
var avg = 0;
for (var i=0; i<data.length; i++) {
for (var b=0; b<data[i].values.length; b++) {
avg += data[i].values[b] / data[i].values.length
}
}
Obviously, this is not correct so I'm turning to you good people for help. Am I approaching this correctly? I'm looking for a solution that is straight JS or maybe a solution involving UnderscoreJS as the actual data structure I'm working with contains over 300 of these objects in a 30k line JSON file.
Thanks in advance..