I am trying to sum a particular field in my extjs store WHERE accountNumber = 'x'. Right now I am looping through and I'm wondering if there is a faster way. Something like var sum = grid.getStore().sum('NameColumn').where('acct') = 12345;
this is how I am doing it now...
for (var i = 0; i < listCount; i++) {
sum = 0;
singleAccount = accountList[i];
//go through calc store, sum pnl column
calculatedPositionsDataStore.each(function (record) {
var tempAcctNum = record.get('AcctNum');
if (tempAcctNum === singleAccount)
{
tempAcctDesc = record.get('AcctShortCode');
tempTradeLevel = record.get('TradingLevel');
var pnl = record.get('CalcPLSett');
sum = sum + pnl;
}
}, this);
There is nothing that's faster than iterating over the whole list, but there's something that's far less to code for you:
var sum = calculatedPositionsDataStore.query("AcctNum", singleAccount).sum("CalcPLSett");
Under the hood, this acts similar to your code, but it's far more readable and reuseable.
So, what's happening here?
store.query
gives you a MixedCollection
containing only those entries where a certain property matches a certain value. You could easily write more complex matching functions if you use queryBy
. To generate this Collection, it runs over all records in the store.sum
, which takes a property name and then iterates over all records in the collection and sums the named property.So it may even be some nanoseconds slower than your code, but if you need it often, in different parts of the code, you save bytes on the wire.