473,783 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subtotal array

I have this array:

[{"acct":"A1","d r":100,"cr":100 },{"acct":"A2", "dr":200,"cr":2 00},
{"acct":"A1","d r":300,"cr",300 },{"acct":"A2", "dr":400,"cr",4 00}]

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 2245
On May 23, 6:35 pm, moua...@yahoo.c om wrote:
I have this array:

[{"acct":"A1","d r":100,"cr":100 },{"acct":"A2", "dr":200,"cr":2 00},
{"acct":"A1","d r":300,"cr",300 },{"acct":"A2", "dr":400,"cr",4 00}]

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,ac c){
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","d r":100,"cr":100 },{"acct":"A2", "dr":200,"cr":2 00},
{"acct":"A1","d r":300,"cr":350 },{"acct":"A2", "dr":400,"cr":4 00}]
alert(getTotal( d,"A1"))

May 24 '07 #2
On May 23, 6:00 pm, "scripts.contac t" <scripts.cont.. .@gmail.com>
wrote:
On May 23, 6:35 pm, moua...@yahoo.c om wrote:I have this array:
[{"acct":"A1","d r":100,"cr":100 },{"acct":"A2", "dr":200,"cr":2 00},
{"acct":"A1","d r":300,"cr",300 },{"acct":"A2", "dr":400,"cr",4 00}]
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,ac c){
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","d r":100,"cr":100 },{"acct":"A2", "dr":200,"cr":2 00},
{"acct":"A1","d r":300,"cr":350 },{"acct":"A2", "dr":400,"cr":4 00}]
alert(getTotal( d,"A1"))
Thanks for your help.
I've decided to use this:

Array.prototype .subtotal=funct ion(grp,field1, field2){
for(var i=0,sum=0;i<thi s.length;i++){
if(this[i][field1]==grp){
sum+=Number(thi s[i][field2].toString().rep lace(/\$|\,/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
1382
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 the XML I'm recieving so that the tax information I'm recieving per item could be grouped and added per state and have the new subtotal node located inside the last node of a particular STORE value. I'm trying to use the Muenchian Method, but I...
7
3927
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 uses InsertAt(myrow,i). When I trace through the sub it is doing the insertat and the value of i is correct. But when I bind my dataset to a datagrid all of my inserted rows are at the end? code is below. What am I doing wrong? thanks, G
2
1732
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 Item ID in the cart to display the appropriate image, artist name, and price information for each item in the cart. I am running into problems when I try and display the subtotal and add a count of each item in the shopping cart. More importantly I...
3
2234
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 displayed as a text box in the main form. However, I am having difficulties in doing this and keep getting #Error or #Name? displayed in it. Any Ideas would be much appreciated. Background info: Form = Tours
5
3977
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 Date Cumulative Sum Daily Sum DC 03-Apr-07 6 6 DC 04-Apr-07 20 14 DC 05-Apr-07 0 0 DC 06-Apr-07 24 4
1
3246
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 First Last Shoes Hats MISC Total WA Seattle 123 01/2007 John Doe 1 5 2 WA Seattle 123 01/2007 John Doe 1 5 2 NY NY 132 01/2007 Jane...
1
3253
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
1945
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 continue on. So, it's possible that out of 100 records, I might reset the subtotal to zero 20 times. I can do this in a report but I need it in query form so I can run it automatically when users log in to let them know if the subtotal has
12
4304
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 =OrderSubform.Form!Subtotal and it seems to work fine. When I try to reference the Subtotal2 text box to perform calculations on it it doesn't work. For instance when in vba I set Me.tax = Me.Subtotal2 * TaxRate I get zero. I tried just saying Me.tax =...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.