| | |
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 |
Is it possible to iterate through an anonymous array of strings with a for loop?
Here's what I'm trying to do: 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
Here's what I'm trying to do:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for ( i in ['country', 'city', 'state'] ) { doSomethingWithString(i); }
Thank you.
--
-- Ghodmode
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.
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.
1
#3 Oct 12th, 2009
•
•
•
•
Is it possible to iterate through an anonymous array of strings with a for loop?
Here's what I'm trying to do: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.JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for ( i in ['country', 'city', 'state'] ) { doSomethingWithString(i); }
Thank you.
--
-- Ghodmode
Therefore you must initiate/create that array before using it at least locally.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for ( i in o = ['country', 'city', 'state'] ){ doSomethingWithString(o[i]); }
A more readable written form of the same with array value alert:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
for ( value in option = ['country', 'city', 'state'] ){ alert( option[ value ] ) }
country
city
state --- I believe this was what you were looking for.
Cheers.
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
javascript Syntax (Toggle Plain Text)
for ( i in ['key1', 'key2']) alert(i); // 1, and 2 respectively // BUT for ( i in {key1:0, key2:0}) 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.
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.
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
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
0
#6 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
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.
![]() |
Similar Threads
- my array is not sorting (C++)
- Delete or Remove Array Element (C++)
- Array Reverse -need help pls (Java)
- anonymous array in method argument (C++)
- Dynamic array of structures (C++)
- display the highest randomly generated number in an array. (C++)
- Setting an array and reading in a txt file backwards (C++)
- Replacing duplicate integers in an array (C#)
- Number of times a number appears in an array (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Modify code created with Dreamweaver CS3, to compare two text feilds.
- Next Thread: Error: Expected ')'
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 iframe images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c website window windowofwords windowsxp wysiwyg \n






