Amount values ​​from an array in JavaScript

I defined JavaScript variables called myData , which is a new Array as follows:

 var myData = new Array(['2013-01-22', 0], ['2013-01-29', 0], ['2013-02-05', 0], ['2013-02-12', 0], ['2013-02-19', 0], ['2013-02-26', 0], ['2013-03-05', 0], ['2013-03-12', 0], ['2013-03-19', 0], ['2013-03-26', 0], ['2013-04-02', 21], ['2013-04-09', 2]); 

I am wondering if it is possible to sum the numerical values ​​found in the array (e.g. 0 + 0 + 21 + 2 + 0, etc.) and probably a variable with the result that I can use outside the script, because I have 7 such arrays corresponding to each day of the week. I want to make a comparison after this based on this. Is this the most preferred method for this kind of action, if possible?

+46
javascript arrays for-loop sum
Apr 17 '13 at 10:28
source share
10 answers

Try to execute

 var myData = [['2013-01-22', 0], ['2013-01-29', 1], ['2013-02-05', 21]]; var myTotal = 0; // Variable to hold your total for(var i = 0, len = myData.length; i < len; i++) { myTotal += myData[i][1]; // Iterate over your first array and then grab the second element add the values up } document.write(myTotal); // 22 in this instance 
+18
Apr 17 '13 at 10:33
source share

You can use the Array.reduce method:

 var sum = myData.reduce( function(sum, current){ return sum + current[1]; }, 0 ); 

See MDN

+90
Apr 17 '13 at 10:38
source share

I think the easiest way is:

 values.reduce(function(a, b){return a+b;}) 
+53
Dec 16 '14 at 1:15
source share

Creating a sum method will work well, for example. you can add the sum function to the array

 Array.prototype.sum = function(selector) { if (typeof selector !== 'function') { selector = function(item) { return item; } } var sum = 0; for (var i = 0; i < this.length; i++) { sum += parseFloat(selector(this[i])); } return sum; }; 

then you could do

 > [1,2,3].sum() 6 

and in your case

 > myData.sum(function(item) { return item[1]; }); 23 

Edit: extending inline functions can be disapproving, because if all of this had been done, we would suddenly override each other (namespace conflicts). you can add a sum function to some module and take an array as an argument if you want. which could mean a signature change to myModule.sum = function(arr, selector) { , then this will become arr

+11
Jun 24 '14 at 1:02
source share

I would use reduce

 var myData = new Array(['2013-01-22', 0], ['2013-01-29', 0], ['2013-02-05', 0], ['2013-02-12', 0], ['2013-02-19', 0], ['2013-02-26', 0], ['2013-03-05', 0], ['2013-03-12', 0], ['2013-03-19', 0], ['2013-03-26', 0], ['2013-04-02', 21], ['2013-04-09', 2]); var sum = myData.reduce(function(a, b) { return a + b[1]; }, 0); $("#result").text(sum); 

Available on jsfiddle

+10
Apr 17 '13 at 10:53 on
source share

Or in ES6

values.reduce((a, b) => a + b),

Example:

 [1,2,3].reduce((a, b)=>a+b) // return 6 
+5
04 Oct '16 at 15:00
source share

If you want to drop the array at the same time as the sum, you can do (say, stack is an array):

 var stack = [1,2,3], sum = 0; while(stack.length > 0) { sum += stack.pop() }; 
+2
Feb 07 '14 at 4:24
source share

You can use your own map method for arrays. map Method (array) (JavaScript)

 var myData = new Array(['2013-01-22', 0], ['2013-01-29', 0], ['2013-02-05', 0], ['2013-02-12', 0], ['2013-02-19', 0], ['2013-02-26', 0], ['2013-03-05', 0], ['2013-03-12', 0], ['2013-03-19', 0], ['2013-03-26', 0], ['2013-04-02', 21], ['2013-04-09', 2]); var a = 0; myData.map( function(aa){ a += aa[1]; return a; }); 

a is your result

+1
Dec 31 '15 at 3:36
source share

The built-in shorthand for javascript arrays is not standard, but you can use underscore.js:

 var data = _.range(10); var sum = _(data).reduce(function(memo, i) {return memo + i}); 

which becomes

 var sumMyData = _(myData).reduce(function(memo, i) {return memo + i[1]}, 0); 

for your business. Look also at fiddle .

0
Nov 11 '13 at 17:35
source share

Old way (if you don't have argument / parameter length)

  >> function sumla1(){ result=0 for(let i=0; i<arguments.length;i++){ result+=arguments[i]; } return result; } >> sumla1(45,67,88); >> 200 

ES6 (array destructuring)

 >> function sumla2(...x){return x.reduce((a,b)=>a+b)} >> >> sumla2(5,5,6,7,8) >> >> 31 >> >> var numbers = [4, 9, 16, 25]; >> sumla2(...numbers); >> 54 
0
Mar 06 '17 at 14:12
source share



All Articles