如何找到数字数组的总和 [英] How to find the sum of an array of numbers
本文介绍了如何找到数字数组的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个数组 [1,2,3,4]
,我怎么能找到它的元素的总和? (在这种情况下,总和将是 10
)。
我想 $。每个
可能是有用的,但我不知道如何实现它。
解决方案
VAR总= 0;
$。每个(ARR,函数(){
总+ =这一点;
});
Given an array [1, 2, 3, 4]
, how can I find the sum of its elements? (In this case, the sum would be 10
.)
I thought $.each
might be useful, but I'm not sure how to implement it.
解决方案
var total = 0;
$.each(arr,function() {
total += this;
});
这篇关于如何找到数字数组的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文