简体   繁体   English

计算对象嵌套数组的平均值-JavaScript

[英]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. 所以我很尴尬地问这个问题,但是,a,我已经达到了我的极限。我有这个数据结构,它是一个嵌套的对象数组,表示接下来3天的时间跨度。 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. 我需要遍历整个数组(我正在处理的数组要比这大得多。),并获得这三天的“ x”平均值,并且我同样需要获取“ y”的平均值时间段。

           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. 我正在寻找一个直接的JS解决方案,或者可能是一个涉及UnderscoreJS的解决方案,因为我正在使用的实际数据结构在30k行JSON文件中包含300多个这些对象。

Thanks in advance.. 提前致谢..

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;
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 小提琴

You could implement it with reduce : 您可以使用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}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM