?
Solved

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

Posted on 2013-05-30
6
Medium Priority
?
337 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
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!

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses

770 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