Solved

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

Posted on 2013-05-30
6
318 Views
Last Modified: 2013-05-30
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
Comment
Question by:nriddock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39208917
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
 

Author Comment

by:nriddock
ID: 39208979
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
 
LVL 25

Expert Comment

by:clockwatcher
ID: 39209059
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:nriddock
ID: 39209102
ok ... that's an idea. Can i use jquery, JS, etc. to reformat/rewrite the multiple "transInfo= " lines into something like you did?
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 39209154
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
 

Author Closing Comment

by:nriddock
ID: 39209171
ok ... thanks for all your help. Greatly appreciated!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
alert on input text 2 25
what should i learn? javascript or typescript? 3 40
Can anyone help me find this preloader? 51 46
Make Float not to Wrap 15 35
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question