473,385 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Subtotal array

I have this array:

[{"acct":"A1","dr":100,"cr":100},{"acct":"A2","dr": 200,"cr":200},
{"acct":"A1","dr":300,"cr",300},{"acct":"A2","dr": 400,"cr",400}]

How would I get the subtotal of each acct so that total for A1 dr/cr
is 400 and total for A2 dr/cr is 600.
TIA...

May 24 '07 #1
2 2221
On May 23, 6:35 pm, moua...@yahoo.com wrote:
I have this array:

[{"acct":"A1","dr":100,"cr":100},{"acct":"A2","dr": 200,"cr":200},
{"acct":"A1","dr":300,"cr",300},{"acct":"A2","dr": 400,"cr",400}]

How would I get the subtotal of each acct so that total for A1 dr/cr
is 400 and total for A2 dr/cr is 600.
-----
function getTotal(arr,acc){
var i=0,n,drtotal=0,crtotal=0;
while(n=arr[i++])
if(n.acct==acc){
drtotal+=n.dr
crtotal+=n.cr
}
return [drtotal,crtotal]
}

-----

Example:
d=[{"acct":"A1","dr":100,"cr":100},{"acct":"A2","dr": 200,"cr":200},
{"acct":"A1","dr":300,"cr":350},{"acct":"A2","dr": 400,"cr":400}]
alert(getTotal(d,"A1"))

May 24 '07 #2
On May 23, 6:00 pm, "scripts.contact" <scripts.cont...@gmail.com>
wrote:
On May 23, 6:35 pm, moua...@yahoo.com wrote:I have this array:
[{"acct":"A1","dr":100,"cr":100},{"acct":"A2","dr": 200,"cr":200},
{"acct":"A1","dr":300,"cr",300},{"acct":"A2","dr": 400,"cr",400}]
How would I get the subtotal of each acct so that total for A1 dr/cr
is 400 and total for A2 dr/cr is 600.

-----
function getTotal(arr,acc){
var i=0,n,drtotal=0,crtotal=0;
while(n=arr[i++])
if(n.acct==acc){
drtotal+=n.dr
crtotal+=n.cr
}
return [drtotal,crtotal]
}

-----

Example:
d=[{"acct":"A1","dr":100,"cr":100},{"acct":"A2","dr": 200,"cr":200},
{"acct":"A1","dr":300,"cr":350},{"acct":"A2","dr": 400,"cr":400}]
alert(getTotal(d,"A1"))
Thanks for your help.
I've decided to use this:

Array.prototype.subtotal=function(grp,field1,field 2){
for(var i=0,sum=0;i<this.length;i++){
if(this[i][field1]==grp){
sum+=Number(this[i][field2].toString().replace(/\$|\,/g,'')) || 0;
}
}
return sum;
}

May 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: J. T. | last post by:
I'm fairly new to XSL, but have made some good progress in picking it up for the project I'm currently working on. I've been trying to figure out how to design a stylesheet that would transform...
7
by: GaryB | last post by:
I have an untyped datatable that has financial numbers and controls that were populated by code (not a simple fill from a DA). Now I want to insert subtotals into it. I wrote a sub to do so that...
2
by: Sparky Arbuckle | last post by:
Hello All! My problem is trying to calculate SubTotal in my FOR EACH NEXT Loop. I am looping through depending on how many of each different item is in the user's shopping cart. I am using each...
3
by: xian2 | last post by:
Hi All, I am trying to create a subtotal text box much like the one in the orders form of the northwind database where you have a subtotal text box in the footer of the subform and then have that...
5
by: mebrabham | last post by:
Hello, I am trying to create a running subtotal in a query and then graph the running subtotal for each city group. The data looks like this in the table (for illustration): My City My...
1
by: tam76131 | last post by:
Hello, I'm very new to access and have been struggling with the subtotal function in access. I have a very large table with the following info State City ID Month ...
1
by: eskelies | last post by:
Hello all, Does anyone have any code that will subtotal a query in Access? Thank you. BEFORE: Account Code Principal Income 4 BUY $10.00 $0.00 4 BUY $10.00 $0.00
2
by: murch.alexander | last post by:
Hi all, I have a query that uses a subquery to subtotal one of its fields. This part is working fine. The twist is that when I encounter certain values, I want to reset the subtotal to zero and...
12
by: etmanage | last post by:
I have a subform that totals a subtotal in the form footer. It works fine. I then pull that value onto the main form into a textbox called Subtotal2 by setting the control source...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.