chrisjordanme
chrisjordanme

Reputation: 612

Calculate average across nested array of objects - JavaScript

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..

Upvotes: 0

Views: 1857

Answers (3)

adeneo
adeneo

Reputation: 318232

var avgx = myArray.map(function(obj) {return obj.values[0].x})
                  .reduce(function(a, b) { return a + b }) / myArray.length;
var avgy = myArray.map(function(obj) {return obj.values[1].y})
                  .reduce(function(a, b) { return a + b }) / myArray.length;

FIDDLE

Upvotes: 2

elclanrs
elclanrs

Reputation: 94101

You could implement it with reduce:

var result = myArray.reduce(function(acc, obj, i) {
  var len = myArray.length;
  acc.x += obj.values[0].x;
  acc.y += obj.values[1].y;
  if (i == len-1) {
    acc.x /= len;
    acc.y /= len;
  }
  return acc;
}, {x:0, y:0});

console.log(result); //=> {x: 9, y: 13.66}

Upvotes: 0

chenglou
chenglou

Reputation: 3640

var totalX = 0;
var totalY = 0;
for (var i = 0; i < myArray.length; i++) {
  totalX += myArray[i].values[0].x;
  totalY += myArray[i].values[1].y;
}

var averageX = totalX / myArray.length;
var averageY = totalY / myArray.length;

Upvotes: 3

Related Questions