[英]I want a for that sum all the results of the 8 values of the array
我想要一个for
数组的 8 个值的所有结果求和的for
循环
total=respuestas[1]+respuestas[2]+respuestas[3]+respuestas[4]+respuestas[5]+respuestas[6]+respuestas[7]+respuestas[8];
All you need is an iterated summation over the indices of the array.您所需要的只是对数组索引的迭代求和。
var total =0;
var i=0;
for( i=1; i<=8;i++){
total += respuestas[i];
}
Another way to write the same thing, using the forEach
loop.使用forEach
循环编写相同内容的另一种方法。
var total = 0 ;
respuestas.forEach(function(entry){
total += entry ;
});
you can try this:你可以试试这个:
var sum = 0; for (i in respuestas) { sum += respuestas[i]; }
sum contains your result. sum 包含您的结果。 The for..in loop will count through all your objects properties (works for arrays as well as you can see). for..in 循环将计算所有对象属性(适用于数组以及您可以看到的)。 I think it looks cleaner than a normal for loop.我认为它看起来比普通的 for 循环更干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.