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