944,099 Members | Top Members by Rank

Ad:
Oct 11th, 2009
0

iterate through an anonymous array?

Expand 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
Similar Threads
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Ghodmode is offline Offline
11 posts
since Sep 2009
Oct 11th, 2009
1
Re: iterate through an anonymous array?
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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Oct 12th, 2009
1
Re: iterate through an anonymous array?
Click to Expand / Collapse  Quote originally posted by Ghodmode ...
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.
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
511 posts
since Jun 2008
Oct 12th, 2009
0
Re: iterate through an anonymous array?
It should be noted, however, that you can hack around it by not using an array but using an object like so
javascript Syntax (Toggle Plain Text)
  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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Oct 13th, 2009
0

garbage collection with JavaScript?

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
Reputation Points: 10
Solved Threads: 3
Newbie Poster
Ghodmode is offline Offline
11 posts
since Sep 2009
Oct 13th, 2009
0
Re: iterate through an anonymous array?
Click to Expand / Collapse  Quote originally posted by Ghodmode ...
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.
Reputation Points: 120
Solved Threads: 61
Posting Pro
Troy III is offline Offline
511 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Modify code created with Dreamweaver CS3, to compare two text feilds.
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Error: Expected ')'





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC