簡體   English   中英

如何在ext js中的for循環內獲取數組中的總值之和

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

在 for 循環中,我正在獲取此數據

該值從 EXT js 存儲中獲取。

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

結果,它顯示

2

5

6

3

我需要一個變量中這個總和的總數。 任何人都可以回答這個,請。 這是一個 javascript function。

您可以為此使用reduce function :

 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)

只需在商店 object 上使用 ExtJS 的內置 function:

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

如果它是一個分組商店,您還可以獲得組的總和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM