iterate through an anonymous array?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Sep 2009
Posts: 11
Reputation: Ghodmode is an unknown quantity at this point 
Solved Threads: 3
Ghodmode's Avatar
Ghodmode Ghodmode is offline Offline
Newbie Poster

iterate through an anonymous array?

 
0
  #1
Oct 11th, 2009
Is it possible to iterate through an anonymous array of strings with a for loop?

Here's what I'm trying to do:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for ( i in ['country', 'city', 'state'] ) {
  2. doSomethingWithString(i);
  3. }
What I want is the string, but what I'm getting is the index number of the array, which I can't use because the array isn't named.

Thank you.

--
-- Ghodmode
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
1
  #2
Oct 11th, 2009
No you can't. i in your scenario is the key for each value so the only way to get the actual value would to do ['val1', 'val2', 'val3'][i] which would defeat the purpose
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 348
Reputation: Troy III will become famous soon enough Troy III will become famous soon enough 
Solved Threads: 42
Troy III's Avatar
Troy III Troy III is offline Offline
Posting Whiz
 
1
  #3
Oct 12th, 2009
Originally Posted by Ghodmode View Post
Is it possible to iterate through an anonymous array of strings with a for loop?

Here's what I'm trying to do:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for ( i in ['country', 'city', 'state'] ) {
  2. doSomethingWithString(i);
  3. }
What I want is the string, but what I'm getting is the index number of the array, which I can't use because the array isn't named.

Thank you.

--
-- Ghodmode
in fact, there is no such thing as anonymous array, this supposedly "arrray" will be garbage collected as soon as the function has passed over.

Therefore you must initiate/create that array before using it at least locally.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for ( i in o = ['country', 'city', 'state'] ){
  2. doSomethingWithString(o[i]);
  3. }

A more readable written form of the same with array value alert:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for ( value in option = ['country', 'city', 'state'] ){
  2. alert( option[ value ] )
  3. }
will serially alert:
country
city
state --- I believe this was what you were looking for.
Cheers.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
0
  #4
Oct 12th, 2009
It should be noted, however, that you can hack around it by not using an array but using an object like so
  1. for ( i in ['key1', 'key2'])
  2. alert(i); // 1, and 2 respectively
  3.  
  4. // BUT
  5.  
  6. for ( i in {key1:0, key2:0})
  7. alert(i); // key1, and key2 respectively
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 11
Reputation: Ghodmode is an unknown quantity at this point 
Solved Threads: 3
Ghodmode's Avatar
Ghodmode Ghodmode is offline Offline
Newbie Poster

garbage collection with JavaScript?

 
0
  #5
Oct 13th, 2009
Thanks to both ShawnCplus and Troy III. Troy's solution is what I was looking for, but using the object notation as Shawn did is an idea with potential, too.

I guess I used the wrong term when I called it an "anonymous array", but I'm glad you got the idea. I was talking about iterating through a list of strings that could be thrown away as soon as I was done going through the list.

This is the first time I've read the term "garbage collection" in the context of JavaScript. I usually only read it in reference to Java, where the process is incredibly well documented. If I think of the browser as the JavaScript equivalent of Java's "virtual machine", is it basically the same process?

Thank you.

--
-- Ghodmode
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 348
Reputation: Troy III will become famous soon enough Troy III will become famous soon enough 
Solved Threads: 42
Troy III's Avatar
Troy III Troy III is offline Offline
Posting Whiz
 
0
  #6
Oct 13th, 2009
Originally Posted by Ghodmode View Post
Thanks to both ShawnCplus and Troy III. Troy's solution is what I was looking for, but using the object notation as Shawn did is an idea with potential, too.

I guess I used the wrong term when I called it an "anonymous array", but I'm glad you got the idea. I was talking about iterating through a list of strings that could be thrown away as soon as I was done going through the list.

This is the first time I've read the term "garbage collection" in the context of JavaScript. I usually only read it in reference to Java, where the process is incredibly well documented. If I think of the browser as the JavaScript equivalent of Java's "virtual machine", is it basically the same process?

Thank you.

--
-- Ghodmode
No,
it is even better!
:')

That's why you never heard of it before.
This one is automatic and doesn't need coders assistance. There are very rare situations when a coder might begin to notice its leaks or his own misuse of objects and vars etc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC