简体   繁体   中英

how to get sum of total value in array inside for loop in ext js

Inside the for loop I m getting this data

This values getting from EXT js store.

  var sum = results.rows.item(i).Count;
  console.log(sum);

in the result, it showing

2

5

6

3

I need a total count of this sum in a single variable. Anybody can answer this, please. It's a javascript function.

You can use the reduce function for that:

 let array = [2,5,6,3]; // will reduce an array to a single variable. let sum = array.reduce((collector, num) => { // the collector is kept for each iteration return collector += num; }, 0 /* initial value is 0 */); console.log(sum)

just use ExtJS's inbuilt function on the store object:

myStore.sum('items'); // assuming 'items' is the field you need to sum

https://docs.sencha.com/extjs/7.0.0/classic/Ext.data.Store.html#method-sum

if it's a grouped store you can also get sum of group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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