简体   繁体   English

如何在数组数组中添加每组数字? (javascript)

[英]How to add each set of numbers within an array of arrays? (javascript)

I'm struggling to design a loop that will cycle through an array of arrays and add each number together, work out the average then output to the console. 我正在努力设计一个循环,该循环将遍历数组数组并将每个数字加在一起,计算出平均值然后输出到控制台。

Here is my code; 这是我的代码;

 var data = [ [3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95], [100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93], [6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80], [6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97], [6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88], [6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99] ] var calcTotal, arrayTotal, totalSum; calcTotal = []; arrayTotal = []; totalSum = []; arrayTotal.push(data[0]) totalSum = data[0].reduce(function(a, b) { return a + b; }); calcTotal.push(totalSum) console.log(Math.round(totalSum / 12)) 

http://plnkr.co/edit/Ses4XApKEdo2CCZmsis7?p=preview http://plnkr.co/edit/Ses4XApKEdo2CCZmsis7?p=preview

So far I have it working to display just one result, Ideally I would output the average from each array when added together in a single array to the console. 到目前为止,我只能显示一个结果。理想情况下,当将单个阵列中的平均值加到控制台时,我将输出每个阵列的平均值。

I've been playing around with for/forEach loops but can't seem to crack it, if anyone can offer some help/advice? 我一直在使用for / forEach循环,但是如果有人可以提供一些帮助/建议,似乎无法破解它?

Thanks 谢谢

It's pretty easy: you can write two functions, add and average , use Array#map and Array#reduce : 这很简单:您可以编写两个函数addaverage ,使用Array#mapArray#reduce

 var data = [ [3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95], [100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93], [6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80], [6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97], [6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88], [6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99] ]; function add(a, b) { return a + b; } function average(list) { return list.reduce(add) / list.length; } document.body.textContent = data.map(average); 

So as far as I understand you need to display to the console average of each row right? 据我了解,您需要向控制台显示每行的平均值,对不对?

You've done pretty good job with single line it just needed to be packed with forEach, here's working fiddle: https://jsfiddle.net/enowacki/dhdc1ztc/2/ 您已经用单行完成了相当不错的工作,只需将其与forEach打包在一起,就可以使用这里的小提琴: https : //jsfiddle.net/enowacki/dhdc1ztc/2/

 const data = [ [3, 6, 14, 17, 30, 40, 44, 66, 69, 84, 92, 95], [100, 17, 26, 28, 29, 34, 38, 59, 78, 82, 84, 93], [6, 12, 22, 25, 35, 44, 45, 57, 60, 61, 78, 80], [6, 11, 14, 19, 33, 50, 57, 58, 61, 88, 89, 97], [6, 13, 23, 28, 39, 44, 50, 55, 58, 72, 80, 88], [6, 8, 22, 26, 48, 50, 55, 65, 77, 84, 93, 99] ]; const result = data.map((arr) => { const rowSum = arr.reduce((prev, curr) => prev + curr); const rowCount = arr.length; const avg = Math.round(rowSum / rowCount); return avg; }); console.log(result); 

I've extracted some additional variables so you can clearly see what's going on feel free to omit them if not needed. 我提取了一些其他变量,以便您可以清楚地看到正在发生的事情,如果不需要,可以随时忽略它们。

Flatten: 展平:

var flat = [].concat.apply([], data);

Sum: 和:

var sum = flat.reduce((a, b) => a+b, 0);
console.log("Avg:" + Math.round(sum / flat.length);

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

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