在JavaScript中,我生成x个数组,全部由57个数字组成。我想计算数组中每个单个数字的平均值,作为一个数组的结果,其平均值为:
array1[0]+array2[0]+array3[0]…./阵列数=[0]的平均值
array1[1]+array2[1]+array3[1]…./阵列数=[1]的平均值
array1[2]+array2[2]+array3[2]…./阵列数=[2]的平均值
这是一个生成阵列的例子:
(5) [Array(57), Array(57), Array(57), Array(57), Array(57)]
0
:
(57) [4, 3, 3, 4, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 4, 3, 5, 5, 4, 4, 4, 4, 3, 4, 3, 4, 3, 4, 4, 4, 4, 3, 4, 3, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 3, 5, 3, 5]
1
:
(57) [1, 3, 1, 2, 1, 3, 2, 2, 3, 1, 2, 1, 3, 2, 3, 1, 4, 4, 4, 4, 4, 3, 1, 1, 4, 3, 3, 2, 2, 2, 4, 3, 3, 3, 3, 2, 3, 3, 3, 1, 2, 1, 1, 2, 2, 4, 1, 4, 1, 1, 3, 4, 3, 2, 1, 2, 4]
2
:
(57) [1, 3, 2, 4, 4, 4, 3, 4, 5, 4, 2, 4, 2, 3, 3, 4, 2, 1, 1, 2, 2, 1, 2, 3, 4, 1, 1, 1, 3, 3, 3, 3, 2, 2, 2, 4, 4, 1, 2, 3, 2, 2, 4, 3, 4, 2, 4, 4, 2, 2, 1, 2, 3, 1, 2, 1, 1]
3
:
(57) [2, 4, 4, 3, 2, 3, 1, 4, 3, 3, 1, 2, 1, 1, 3, 4, 4, 2, 3, 2, 2, 1, 2, 2, 4, 1, 2, 1, 1, 4, 2, 3, 3, 3, 1, 4, 1, 4, 1, 4, 4, 4, 1, 1, 3, 3, 3, 4, 1, 3, 2, 1, 4, 1, 1, 2, 4]
4
:
(57) [1, 1, 3, 1, 1, 2, 4, 4, 5, 1, 2, 1, 2, 2, 3, 4, 4, 2, 1, 3, 1, 2, 4, 3, 4, 4, 4, 4, 1, 3, 1, 2, 2, 4, 1, 4, 1, 3, 3, 4, 1, 3, 4, 2, 4, 1, 4, 3, 4, 1, 2, 4, 4, 1, 1, 4, 4]
length
:
5
[[Prototype]]
:
Array(0)
有人能给我举个例子,让我正确地构建这个解决方案吗?
假设rawArrays是您生成的数字数组数组。
const amountOfArrays = 2, amountOfNumbersInArray = 3;
const rawArrays = [
[1, 2, 3],
[4, 5, 6],
];
const averagesOnArrayIndex = rawArrays[0].map((_, id) => {
const sumOfElementsOnThisId = rawArrays.map((array) => array[id]).reduce((acc, cur) => acc += cur, 0);
const averageOnThisId = sumOfElementsOnThisId / amountOfArrays;
return averageOnThisId;
});