[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

help with javascript array - need to find the X type of item

hello,

i have the following javascript array(s) of data being returned from the database
       transInfo =  {
                    'id' : "404828",
                    'type' : "Credit Limit",
                    'amount' : "$10,000.00",
                    'officerName' : "Billy Batson",
                    'officerCode' : "01160",
                    'officerEid' : "ABC123",
                    'officerPhone' : "(444) 555-1111",
                };
            
        transInfo =  {
                    'id' : "404829",
                    'type' : "Debit Limit",
                    'amount' : "$20,000.00",
                    'officerName' : "Susan Smith",
                    'officerCode' : "1213456789",
                    'officerEid' : "987654321",
                    'officerPhone' : "(804)123-4567",
                };
            
           
          transInfo =  {
                    
                    'id' : "404860",
                    'type' : "Debit Limit",
                    'amount' : "$0.00",
                    'officerName' : "Super Man",
                    'officerCode' : "111111",
                    'officerEid' : "999999",
                    'officerPhone' : "(804)741-7417",
                };
           
           transInfo =  {
                    'id' : "404861",
		'type' : "Debit Limit",
                    'amount' : "$225,151.24",
                    'officerName' : "Last One",
                    'officerCode' : "999999",
                    'officerEid' : "90909090",
                    'officerPhone' : "",
                };
            
            transInfo =  {
                    
                    'id' : "404862",
                    'type' : "Credit Limit",
                    'amount' : "$563,465.56",
                    'officerName' : "Willy Williams",
                    'officerCode' : "789789",
                    'officerEid' : "987987",
                    'officerPhone' : "(741)741-7417",
                };

Open in new window


i need to display the officer info (name, phone, code, etc.) from the First "type" where type= Debit Limit.

Any suggestions?
0
nriddock
Asked:
nriddock
  • 3
  • 3
1 Solution
 
clockwatcherCommented:
You don't have an array of objects there.  You're just replacing a single transinfo object instance with another.  

Did you mean?
  transInfo =  [
              {
                    'id' : "404828",
                    'type' : "Credit Limit",
                    'amount' : "$10,000.00",
                    'officerName' : "Billy Batson",
                    'officerCode' : "01160",
                    'officerEid' : "ABC123",
                    'officerPhone' : "(444) 555-1111",
                },
               {
                    'id' : "404829",
                    'type' : "Debit Limit",
                    'amount' : "$20,000.00",
                    'officerName' : "Susan Smith",
                    'officerCode' : "1213456789",
                    'officerEid' : "987654321",
                    'officerPhone' : "(804)123-4567",
                },
                {
                    
                    'id' : "404860",
                    'type' : "Debit Limit",
                    'amount' : "$0.00",
                    'officerName' : "Super Man",
                    'officerCode' : "111111",
                    'officerEid' : "999999",
                    'officerPhone' : "(804)741-7417",
                },
               {
                    'id' : "404861",
		'type' : "Debit Limit",
                    'amount' : "$225,151.24",
                    'officerName' : "Last One",
                    'officerCode' : "999999",
                    'officerEid' : "90909090",
                    'officerPhone' : "",
                },
               {
                    
                    'id' : "404862",
                    'type' : "Credit Limit",
                    'amount' : "$563,465.56",
                    'officerName' : "Willy Williams",
                    'officerCode' : "789789",
                    'officerEid' : "987987",
                    'officerPhone' : "(741)741-7417",
                } 
            ];

Open in new window


If the above isn't what you mean, can you better explain how you have your data stored?
0
 
nriddockAuthor Commented:
In our main apsx page we have code that queries the database and returns the data into an array based on "user defined tokens"

transInfo = {
                    'id' : "{^%IntegrationCode%^}",
	                'type' : "{^%SubType%^}",
                    'amount' : "{^%Amount%^}",
                    'officerName' : "{^%OfficerName%^} ",
                    'officerCode' : "{^%OfficerCode%^} "
                };

Open in new window


It loops through the data and for each record , data set, etc. it assigns it to a "transInfo" array, bucket, lump, etc.

 Thus if we have multiple records, you'll get the fumble of data i described above.
0
 
clockwatcherCommented:
That's what I was afraid of.  You'll need to change that because as I mentioned it's just overwriting your variable.  It's not actually giving you a javascript array.  

If you don't need it as an array and only care about one, you'd need to change the server-side code so it selects the one you want and only spits out that one.  If you actually do need all of them in your page, then you'll need to change the code so it spits out an array of objects -- in something similar to the format that I showed above.

With what you're currently have, from the javascript side of things, you won't be able to work with anything but the last one that your aspx is spitting  out.  You're essentially doing this:

x = 3
x = 2
x = 7
x = 32

That's not an array.  You're overwriting a single variable.

Post your aspx code.  If it's easier, rather than changing your code to produce something like the above, you could simply add an append.  In other words, change your aspx code to something more like this:

var transInfoObjects = [];

<%
foreach (whatever in mycollection) {
     response.write(whatever.toJSON());  // whatever code dumps out your transInfo obj
     response.write("transInfoObjects.append(transInfo);");  // add this line to append it to an array
}
%>

Or something like this:

var transInfo = [];
transInfo[0] = { ... }
transInfo[1] = { ... }

If you post your aspx code it'd be easier to recommend something.

Until you actually have it stored on the javascript side in a useable format (and you're not just overwriting your variable), you won't be able to do anything with it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nriddockAuthor Commented:
ok ... that's an idea. Can i use jquery, JS, etc. to reformat/rewrite the multiple "transInfo= " lines into something like you did?
0
 
clockwatcherCommented:
If you're using ajax to pull it in then you could do some text manipulation on it before you eval'd it.  But if it's actually embedded in your page (which I'm guessing that it is), then no.  They're simply lines of code that are going to get executed by the browser.
0
 
nriddockAuthor Commented:
ok ... thanks for all your help. Greatly appreciated!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now