array.map()的二维数组列的平均值 [英] Average of bidimensional array's columns with array.map()

查看:127
本文介绍了array.map()的二维数组列的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数组:

  var array = [[1,3,9],
[4,6,8],
[3,7,5],
[2,8,4]];

我想得到每列的平均数,但现在我只是想总结它们。这是我的代码:

  var sum = function(arr){
return arr.reduce(function(a, b){return a + b;},0);
};

var tests = array.map(function(v,i){
return sum(array.map(function(v){return v [i];}))
});

返回测试;

输出正确地转化了总和,但它似乎与行数一样多( 4行),而不是3对应于列。这是输出:

  tests = [10,24,26,NULL] 

任何想法为什么会发生这种情况?



编辑:
我怎样才能执行只有多少列的计算?

我正在使用 Nenad 的答案给出正确的结果。但是我需要在Google Sheets的脚本编辑器中实现它,它似乎并不了解用=>缩短的功能。我替换了较长版本的缩短片段,但我没有得到相同的结果。



  var array = [[1,3,9],[4,6,8],[3,7,5],[2,8,4]]; var sums = array.reduce(function (r,e,i){e.forEach(function(a,j){r [j] =(r [j] || 0)+ a; if(i == array.length-1){r = r.map(function(el){return el / array.length;});}}); return r;},[])console.log(sums);  



我没有看到这与缩短版本有什么区别,但这一个返回:

  sums = [0.15625,0.75,1.34375]; 

而不是:

  sums = [2.5,6,6.5]; 

总和是正确的,但是当我将el / array.length或者el / 4,结果是这3个奇怪的数字。我不明白那些来自哪里。你可以使用 reduce()来解决这个问题。

forEach()来返回结果。

  var array = [[1,3,9],[4,6,8],[3,7,5],[2,8,4]]; var sums = array.reduce(函数(r,e,i){e.forEach((a,j)=> r [j] =(r [j] || 0)+ a)return r;},[])console.log总和) 



要计算每列的平均值,您可以添加<$

map() -hide =falsedata-console =truedata-babel =false>

  var array = [[1,3,9],[4,6,8],[3,7,5],[2,8,4]]; var sums = array.reduce(function (r,e,i){e.forEach((a, j)=> r [j] =(r [j] || 0)+ a)if(i == array.length  -  1)r = r.map(el => el / array.length); 

I have an array that looks like this:

var array = [ [1,3,9],
              [4,6,8],
              [3,7,5],
              [2,8,4] ];

I want to get the average number of each column, but for now I was just trying to sum them. This is my code:

var sum = function(arr) {
  return arr.reduce(function(a, b) { return a + b; }, 0);
};

var tests = array.map(function(v, i) {
  return sum(array.map(function(v) { return v[i]; }))
});

return tests;

The output turns the sum correctly, but it seems to be doing as many sums as there are rows (4 rows), instead of 3 corresponding to the columns. This is the output:

tests = [10, 24, 26, NULL]

Any idea why is this happening? How can I perform the calculation only for as many columns as there are instead of rows?

EDIT:

I'm using Nenad's answer which gives the correct result. But I need to implement it on Google Sheets's Script Editor, which doesn't seem to understand the shortened functions with "=>". I replaced the shortened pieces for the longer version, but I'm not getting the same result.

var array = [ [1,3,9],
              [4,6,8],
              [3,7,5],
              [2,8,4] ];

var sums = array.reduce(function(r, e, i) {
    e.forEach(function(a,j) { r[j] = (r[j] || 0) + a;
                             if (i == array.length-1) { r = r.map(function(el){ return el/array.length; }); }
                            });
    return r;
  }, [])
  
console.log(sums);  

I don't see any difference between this and the shortened version, yet this one returns:

sums = [0.15625, 0.75, 1.34375];

Instead of:

sums = [2.5, 6, 6.5];

The sum is done correctly, but when I divide "el/array.length" or even "el/4", the result are these 3 weird numbers. I don't understand where are those coming from. Where did I go wrong?

解决方案

You can use reduce() and forEach() to return result.

var array = [
  [1, 3, 9],
  [4, 6, 8],
  [3, 7, 5],
  [2, 8, 4]
];

var sums = array.reduce(function(r, e, i) {
  e.forEach((a, j) => r[j] = (r[j] || 0) + a)
  return r;
}, [])

console.log(sums)

To calculate avg for each column you can add map() on last iteration of array.

var array = [
  [1, 3, 9],
  [4, 6, 8],
  [3, 7, 5],
  [2, 8, 4]
];

var sums = array.reduce(function(r, e, i) {
  e.forEach((a, j) => r[j] = (r[j] || 0) + a)
  if (i == array.length - 1) r = r.map(el => el / array.length);
  return r;
}, [])

console.log(sums)

这篇关于array.map()的二维数组列的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆