简体   繁体   English

数组中所有元素的总和

[英]The sum of all the elements in an Array

i am trying to do some examples. 我试图做一些例子。 i want to sum all the elements of the array here is my code 我想对数组的所有元素求和,这是我的代码

var marks = [10, 20, 30, 40];
var total = 0;

for (var singleMark in marks) {
    total += marks;

}
console.log("the total marks re " + total);

But the results displayed is the total marks re 010,20,30,4010,20,30,4010,20,30,4010,20,30,40 但显示的结果是总计分数010,20,30,4010,20,30,4010,20,30,4010,20,30,40

Please help me and tell me where am i doing it wrong.Thanks. 请帮助我,告诉我我在哪里做错了。谢谢。 I can take the for loop but i want to learn using foreach as in Javascript the definite guide book it says so 我可以使用for循环,但是我想像Javascript中那样学习使用foreach,因为它说的是明确的指南

try like this 这样尝试

var marks = [10, 20, 30, 40];
var sum=marks.reduce(function(a, b){return a+b;})

From @thomas comment 来自@thomas评论

ES6 syntax you can also write: 您还可以编写ES6语法:

[10, 20, 30, 40].reduce((a,b) => a + b)

For in will go over properties of objects. 对于in将遍历对象的属性。 If you where to use for in on an array it will return you the index it is iterating over. 如果您要在数组上使用in,它将返回迭代的索引。

You should be using forEach to iterate over arrays. 您应该使用forEach遍历数组。

marks.forEach(function(element, index){
    total+=element;
});

The element value is the element from the array, while the index value is the index that forEach is currently at. 元素值是数组中的element ,而index值是forEach当前所在的索引。 index is optional and does not need to be included. index是可选的,不需要包括在内。

Why don't you use conventional for loop ? 为什么不使用常规的for循环?

var marks = [10, 20, 30, 40];
var total = 0;
for (var i = 0, iLen = marks.length; i < iLen; i++) {
    total += marks[i];
}
console.log("the total marks re " + total);

You can use reduce too. 您也可以使用reduce The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. reduce()方法对一个累加器应用一个函数,该数组的每个值(从左到右)将其减小为单个值。

var total = [0, 1, 2, 3].reduce(function(a, b) {
  return a + b;
});
// total == 6

Do not use for .. in for traversing in array. 不要将for .. in用于数组for .. in的遍历。 Use for(..;..;..) for(..;..;..)

The values are represented as strings in this way. 值以这种方式表示为字符串。

For reference, safer way: 供参考,更安全的方法:

for(var i = 0; i< arr.length; i++){
    sum += arr[i];
}
you can use the following two ways

var marks = [10, 20, 30, 40];
        var total = 0;
        marks.forEach(function addNumbers(value) {
            total += value;
        });
        console.log(total);

or
var marks = [10, 20, 30, 40];
        var total = 0;
        var singleMark;
        for (var i = 0; i < marks.length;i++) {
            total = total + marks[i];

        }
        console.log("the total marks re " + total);

Here's a more high-order and readable approach: 这是一种更高级,更易读的方法:

marks.reduce(Math.sum, 0);

The reduce function is used along with the Math.sum and does this: foreach element of marks, use math.sum(cur, marks[i]); reduce函数与Math.sum一起使用,并执行以下操作:对于标记的每个元素,使用math.sum(cur, marks[i]); to take the current element and the next one; 接受当前元素和下一个元素; do this until all of the elements in the array have been passed to the sum function and have been added up like this. 这样做直到将数组中的所有元素都传递给sum函数并像这样被累加起来。 Hope this helps. 希望这可以帮助。

You're just using the wrong variable marks instead of singleMark in your loop. 您只是在循环中使用了错误的变量marks而不是singleMark Try this: 尝试这个:

for (var singleMark in marks) {
    total += marks[singleMark];
}

Edit: However it is bad practice to use in to iterate through arrays. 编辑:但是,使用in来遍历数组是一种不好的做法。 Consider using javascript Array.prototype functions like .reduce() or .forEach() 考虑使用javascript Array.prototype功能等.reduce().forEach()

Its Easy. 这很容易。

Create a function with a parameter. 创建带有参数的函数。

Inside the function loop through the array Till the loop is finished. 在遍历数组的函数循环内部直到循环结束。

Inside the Loop add the elements. 在循环内部添加元素。

function sumArray(arra){

    var sum=0;
    for (var i = 0; i <arra.length; i++) {
        sum= sum+arra[i];
    }
}

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

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