Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2013-05-30
6
Medium Priority
?
352 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
  • 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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 

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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

606 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