Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #37423 > unrolled thread
| Started by | groovee@cyberdude.com |
|---|---|
| First post | 2019-09-26 23:55 -0700 |
| Last post | 2019-09-27 02:40 -0700 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.javascript
Adding a variable to a variable NAME groovee@cyberdude.com - 2019-09-26 23:55 -0700
Re: Adding a variable to a variable NAME Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-09-27 00:25 -0700
Re: Adding a variable to a variable NAME dr.j.r.stockton@gmail.com - 2019-09-27 02:40 -0700
| From | groovee@cyberdude.com |
|---|---|
| Date | 2019-09-26 23:55 -0700 |
| Subject | Adding a variable to a variable NAME |
| Message-ID | <87e082ab-26c6-4b36-b5fd-21785c59a872@googlegroups.com> |
If I want a series of variables, like this:
var1 = "groovee";
var2 = "groovee";
var3 = "groovee";
...and so on, how would I do it? I will obviously use a loop like this:
for (i=1; i = 3; i++) { blah blah
but how would I assign the NAME of the variable to be "var" + the loop variable? ie. var1, var 2, so on?
PHP calls this $$x, unless I'm mistaken - how does *JS* do it? :)
Thanks.
[toc] | [next] | [standalone]
| From | Jonas Thörnvall <jonas.thornvall@gmail.com> |
|---|---|
| Date | 2019-09-27 00:25 -0700 |
| Message-ID | <4e143240-9342-4187-8607-d2d6c81714da@googlegroups.com> |
| In reply to | #37423 |
Den fredag 27 september 2019 kl. 08:55:28 UTC+2 skrev gro...@cyberdude.com:
> If I want a series of variables, like this:
>
>
> var1 = "groovee";
> var2 = "groovee";
> var3 = "groovee";
>
> ...and so on, how would I do it? I will obviously use a loop like this:
>
> for (i=1; i = 3; i++) { blah blah
>
> but how would I assign the NAME of the variable to be "var" + the loop variable? ie. var1, var 2, so on?
>
>
> PHP calls this $$x, unless I'm mistaken - how does *JS* do it? :)
>
> Thanks.
I saw someone suggested this, seems easy.
for (var i = 0; i < coords.length; ++i) {
var str ="var"+ i+" = groovee";
eval(str);
}
But someone else said one really should use arrays instead var[i]=groovee
[toc] | [prev] | [next] | [standalone]
| From | dr.j.r.stockton@gmail.com |
|---|---|
| Date | 2019-09-27 02:40 -0700 |
| Message-ID | <fbcdc47a-5199-4102-8efd-5afe9e1226a2@googlegroups.com> |
| In reply to | #37423 |
On Friday, 27 September 2019 07:55:28 UTC+1, gro...@cyberdude.com wrote: > ... > but how would I assign the NAME of the variable to be "var" + the loop variable? ie. var1, var 2, so on? > > > PHP calls this $$x, unless I'm mistaken - how does *JS* do it? :) UNTESTED - but something like for (i ... ) window["var"+i] = Str, and for variables local to a function use the name of the function ?? ?? -- (c) John Stockton, near London, UK. Using Google Groups. | Mail: J.R.""""""""@physics.org - or as Reply-To, if any. |
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web