By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

make array1 array2 inner a cicle for

P: n/a
I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

how I can to create the names of the variable depending by index i?
z+i not work;

Aug 26 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
padew wrote on 26 aug 2006 in comp.lang.javascript:
I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a
z2 ?
>
how I can to create the names of the variable depending by index i?
z+i not work;
for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')

Beware: eval() is evil.

=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'

try:

var z = new array('a','a','a')

or shorter:

var z = ['a','a','a']

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #2

P: n/a
Il 26 Aug 2006 20:44:09 GMT, Evertjan. ha scritto:
padew wrote on 26 aug 2006 in comp.lang.javascript:
>I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

z2 ?
nobody is perfect :-)

>>
how I can to create the names of the variable depending by index i?
z+i not work;

for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')
ok your example is for:
var z+i="a";

but for
z+i=new Array("a");
is so:
eval( 'var z'+i+' =new Array ("a");')

and I have the object new Array?
=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'
not so;
Aug 26 '06 #3

P: n/a
padew wrote on 26 aug 2006 in comp.lang.javascript:
Il 26 Aug 2006 20:44:09 GMT, Evertjan. ha scritto:
>padew wrote on 26 aug 2006 in comp.lang.javascript:
>>I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

z2 ?
nobody is perfect :-)
Probably so, but your Q is as it is,
so please agnowledge [or not] my surmized correction of it.

"fo to have" is a riddle too.
>>>
how I can to create the names of the variable depending by index i?
z+i not work;

for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')

ok your example is for:
var z+i="a";

but for
z+i=new Array("a");
is so:
eval( 'var z'+i+' =new Array ("a");')

and I have the object new Array?
What do you think, want, and what is the result of you testing?
>
>=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'

not so;
A pity.

Your requested naming seems rather pointless.

An array is the perfect and time trusted way
to make a group of variables with a logical connected naming system.

var z = [new Array ('a'),new Array ('a'),new Array ('a')]

or in short:

var z = [['a'],['a'],['a']]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #4

P: n/a
var z = [['a'],['a'],['a']]
ok the result is this but I have this problem:
the number of every elements ['a'] isn't costant;
is the result of a:

for (var i = 0; i<x.lenght; i++)
var z = [ if lengh 4 here 4 elemts ['a'] ]
only one question:
you use ' ' and not ""; are there difference?

---------------------
for the other
is difficult to communicate without tone voice and/or expression face;
often it has been misinterpreted;
with aggravating circumstance with one does not know well English;
( for to avoid misinterpreted am IK )

ok does test and seem work; but test is only with
simple data;

Aug 26 '06 #5

P: n/a
padew wrote on 27 aug 2006 in comp.lang.javascript:
>var z = [['a'],['a'],['a']]


ok the result is this but I have this problem:
the number of every elements ['a'] isn't costant;
is the result of a:

for (var i = 0; i<x.lenght; i++)
When quoting code, copy and paste, do not type.

Language mistakes 'costant' [constant] are acceptable,
coding mistakes 'lenght' in stead of 'length' are not,
since they show an error that should have been corrected in your code.
var z = [ if lengh 4 here 4 elemts ['a'] ]
use:

var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]

This is basic javascript, as shown in any tutorial.
only one question:
you use ' ' and not ""; are there difference?
They can both be used.

This is basic javascript, as shown in any tutorial.

---------------------
for the other
is difficult to communicate without tone voice and/or expression face;
Communication is going on on usenet since 1979, years before the start of
the www, and still going strong.
often it has been misinterpreted;
Yes, but that is the way of things, come over that, no one minds
mistakes, but they should be exposted where they hamper the understanding
of the communication.
with aggravating circumstance with one does not know well English;
My language is Netherlands/Dutch.
( for to avoid misinterpreted am IK )
sorry, "IK" givers no clue. Are you Italian, as the header suggests?
ok does test and seem work; but test is only with
simple data;
If that is enough for you?

PS: <http://javascript.html.it/guide/lezione/850/array/>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #6

P: n/a
var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]
that you write is:
var z = ['a','a','a']
that I want is:
var z = [['a'],['a'],['a']]

var z = new array()
z[0] = new array()
z[1] = new array()
etc. [any length]
i think, but no sure if I can use in a cycle for ..

ok for all
Aug 26 '06 #7

P: n/a
padew <aa******************@xcxc.xxwrote in comp.lang.javascript:
>var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]

that you write is:
var z = ['a','a','a']
that I want is:
var z = [['a'],['a'],['a']]

var z = new array()
z[0] = new array()
z[1] = new array()
etc. [any length]
i think, but no sure if I can use in a cycle for ..
You want to make an array of arrays (also called a two-dimensional array).

Well yes, an element of an array object can be an array object. And it
can go on ad infinitum.

By 'cycle', do you mean you want to make it in a for-loop??

Easily done:

/* MY_ARRAY_LENGTH is a constant of your choosing,
defined in script or at runtime */
var myArray = new Array(MY_ARRAY_LENGTH);
for (var i = 0; i < myArray.length; i++)
myArray[i] = new Array("a");

You should end up with an array (object) named 'myArray' having
MY_ARRAY_LENGTH count of elements, each element being an array (object)
containing ONE element, being a string constant having a one-character
long string with the value of 'a'.

You could make essentially N - 1 nested for-loops to produce an N-
dimensional array, if within each for-loop, you defined an element of the
existing array to hold a newly constructed array. Do it if there is a
real purpose to doing it.
Notes:

1. The constructor for an array object is "Array()" and NOT "array()"
(See the spelling-counts remark below.)
2. Those familiar with C/C++ who happen to code in Javascript make use of
a great many conventions used by programmers of C/C++. While 'a' and "a"
are optional ways of expressing string constants in Javascript, this is
not so in C and C++, in which the former expresses a character byte value
and the latter is a null-terminated string. It's probably a force of
habit of these programmers to use 'x' for single character constant
definition and "xyz" for multi-character string constant definition.
Javascript is essentially derived from C and C++ and so those familiar
with C/C++ bring their habits/conventions to Javascript.

By the way, you may have noticed that spelling counts in Javascript and
every other scripting/programming language on the planet. Typos are the
very things that break code.

Evertjan is exactly right: rather than re-type code, do a copy-and-paste
of the code that you are working with. It is often typos (including and
especially case sensitivity) that confound programmers in their days-long
efforts to find out why code/script is not executing the way they want.

Many of the questions you pose could be answered if you read a
programmer's reference and/or tutorial on core Javascript.

>
ok for all

Aug 27 '06 #8

P: n/a
JRS: In article <1d****************************@40tude.net>, dated Sun,
27 Aug 2006 00:05:09 remote, seen in news:comp.lang.javascript, padew
<aa******************@xcxc.xxposted :
>for the other
is difficult to communicate without tone voice and/or expression face;
often it has been misinterpreted;
with aggravating circumstance with one does not know well English;

Then ask also in Italian. Many civilised people have a knowledge of the
language, at least for reading it; and some know it well. All you need
is for one person to understand the question properly and answer it; it
does not matter if some of those in the land injudiciously (& possibly)
discovered by Signor Vespucci fail to comprehend the Italian and are
forced to resort to the "English" version.

Also, news:it.comp.lang.javascript is active.
Consider

var z = []
for (var i = 0; i<4; i++) z[i] = "a"+i
Read the newsgroup FAQ.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 27 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.