# 如何find一个数组的总和

(reduce #'+ '(1 2 3)) ; 6

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

var sum = [1, 2, 3].reduce((a, b) => a + b, 0); console.log(sum); // 6

var a = [1,2,3]; var sum = a.reduce(function(a, b) { return a + b; }, 0);
var arr = [1,2,3,4]; var total=0; for(var i in arr) { total += arr[i]; }
var total = 0; \$.each(arr,function() { total += this; });

var array = [1, 2, 3]; for (var i = 0, sum = 0; i < array.length; sum += array[i++]) ;

JavaScript不知道块的范围，所以sum将是可访问的：

console.log(sum); // => 6

function sumArray(array) { for ( var index = 0, // The iterator length = array.length, // Cache the array length sum = 0; // The total amount index < length; // The "for"-loop condition sum += array[index++] // Add number on each iteration ); return sum; }
arr.reduce(function (a, b) { return a + b; });

// Given array 'arr' var i = arr.length; var sum = 0; while (--i) sum += arr[i];

[1,2,3,4,5,6].reduceRight(function(a,b){return a+b;})

array = [1, 2, 3, 4]; sum = _.sum(array);//10

sum= arr.reduce(function (a, b) {return a + b;}, 0);

eval([1,2,3].join("+"))

var addition = []; addition.push(2); addition.push(3); var total = 0; for (var i = 0; i < addition.length; i++) { total += addition[i]; } alert(total); // Just to output an example /* console.log(total); // Just to output an example with Firebug */

var myNumbers = [1,2,3,4,5] var total = 0; for(i=0; i<myArray.length; i++){ total += myArray[i]; }

var numbers = [1,2,3,4]; var totalAmount = 0; for (var x = 0; x < numbers.length; x++) { totalAmount += numbers[x]; } console.log(totalAmount); //10 (1+2+3+4)

## 推荐的方法

[1,2,3].reduce(function(acc, val) { return acc + val; });

## 危险的方式

eval([1,2,3].join('+'));

var totally = eval(arr.join('+'))

var arr = ['(1/3)','Date.now()','foo','bar()',1,2,3,4]

function arraySum(array){ var total = 0, len = array.length; for (var i = 0; i < len; i++){ total += array[i]; } return total; }; var my_array = [1,2,3,4]; // Returns 10 console.log( arraySum( my_array ) );

function sumArray(arr) { var total = 0; arr.forEach(function(element){ total += element; }) return total; } var sum = sumArray([1,2,3,4]) console.log(sum)
Object.defineProperty(Object.prototype, 'sum', { enumerable:false, value:function() { var t=0;for(var i in this) if (!isNaN(this[i])) t+=this[i]; return t; } }); [20,25,27.1].sum() // 72.1 [10,"forty-two",23].sum() // 33 [Math.PI,0,-1,1].sum() // 3.141592653589793 [Math.PI,Math.E,-1000000000].sum() // -999999994.1401255 o = {a:1,b:31,c:"roffelz",someOtherProperty:21.52} console.log(o.sum()); // 53.519999999999996

let arr = [1, 2, 3, 4]; let sum = arr.reduce((v, i) => (v + i)); console.log(sum);

function arrSum(arr){ total = 0; arr.forEach(function(key){ total = total + key; }); return total; }

> a = [1,2,3,4]; a.foo = 5; a['bar'] = 6; sum = 0; a.forEach(function(e){sum += e}); sum 10 > a = [1,2,3,4]; a.foo = 5; a['bar'] = 6; sum = 0; a.forEach(e => sum += e); sum 10 > a = [1,2,3,4]; a.foo = 5; a['bar'] = 6; sum = 0; for(e in a) sum += e; sum "00123foobar" > a = [1,2,3,4]; a.foo = 5; a['bar'] = 6; sum = 0; for(e of a) sum += e; sum 10

Array.prototype.sum = function() {return [].reduce.call(this, (a,i) => a+i, 0);}

var sum = (arr) => arr.length === 1 ? arr[0] : arr.shift() + sum(arr); sum([1,2,3,4]) // 10

var size = A.length; var sum = (size + 1) * (size + 2) / 2;

var sum = 0; arr.map(function(item){ sum += item; }); // sum now contains the total.

Array.prototype.sum = function(){ var sum = 0; this.map(function(item){ sum += item; }); return sum; }

arr.sum();