Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #36664

Re: Create array using name from string?

From "R.Wieser" <address@not.available>
Newsgroups comp.lang.javascript
Subject Re: Create array using name from string?
Date 2019-05-29 09:35 +0200
Organization Aioe.org NNTP Server
Message-ID <qclco8$1v13$1@gioia.aioe.org> (permalink)
References <38042cf3-73ca-403a-b13b-7bcfbec21ffb@googlegroups.com> <qcivl4$119l$1@gioia.aioe.org> <qcj10g$17lk$1@gioia.aioe.org> <84fe9111-5bab-4537-8d08-424ec56763d7@googlegroups.com> <f303a7bf-78b4-48c4-8e96-dd3e72992961@googlegroups.com>

Show all headers | View raw


Jonas,

> What does the follwing eval actually mean, it is not clear to me?
> eval("dynamic" + i + " = val[i]");

It will create a /variable/ with the name "dynamic" and whatever value(?) is 
in "i".   Assuming that "i" contains 42 than the name will be "dynamic42". 
After that it will simply copy the contents of "val[i]" into it.

> Will it create an array named dynamic[] that can be accessed like any 
> array?

Nope.

But that is easily solved.  John has already shown how to create an array. 
The only thing you need to do is to remove the initialisation values from it 
(rewritten to match your example) :

i=42
eval("var dynamic" + i + " = []");

(translates to 'var dynamic42 = []')

or, to use Johns example, but without initializing the array.

s = "banana"
eval("var " + s + " = []");

This will create an empty array with the name "banana", just like

var banana = [];

would.

Hope that helps.

Regards,
Rudy Wieser

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-28 02:06 -0700
  Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-28 02:11 -0700
  Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-28 11:39 +0200
    Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-28 12:02 +0200
      Re: Create array using name from string? dr.j.r.stockton@gmail.com - 2019-05-28 05:38 -0700
        Re: Create array using name from string? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-05-28 17:06 +0100
          Re: Create array using name from string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-29 00:22 +0200
            Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 00:07 -0700
              Re: Create array using name from string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-05-29 09:47 +0200
              Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-29 10:10 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:10 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:12 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:22 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-30 11:28 -0700
            Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-29 10:24 -0700
              Re: Create array using name from string? Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2019-05-30 11:37 +0200
                Re: Create array using name from string? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2019-05-30 12:35 +0200
                Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-30 03:43 -0700
                Re: Create array using name from string? Julio Di Egidio <julio@diegidio.name> - 2019-05-30 03:48 -0700
                Re: Create array using name from string? Gene Wirchenko <genew@telus.net> - 2019-05-30 12:13 -0700
                Re: Create array using name from string? "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2019-05-30 10:34 -0700
              Re: Create array using name from string? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-05-31 19:01 +0100
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-31 11:21 -0700
                Re: Create array using name from string? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2019-06-01 00:31 +0200
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-06-01 00:40 -0700
                Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-06-01 01:10 -0700
                Re: Create array using name from string? John G Harris <niam@jghnorth.org.uk.invalid> - 2019-06-01 10:14 +0100
        Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 00:15 -0700
          Re: Create array using name from string? "R.Wieser" <address@not.available> - 2019-05-29 09:35 +0200
            Re: Create array using name from string? Jonas Thörnvall <jonas.thornvall@gmail.com> - 2019-05-29 01:16 -0700

csiph-web