0

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

1
  • One average for each value key (i.e. avg for x, avg for y) or an avg of all values? Commented May 7, 2014 at 3:56

3 Answers 3

3
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;
Sign up to request clarification or add additional context in comments.

Comments

2
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

Comments

0

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.