2

I was trying to create an array which contains 3 fixed fields.
So, something like

var x0 = new Array(x,y,z);

Now the thing is there can be multiple arrays like x[], which have the different values.
So something like

   var x1 = new Array(x,y,z); x2 = new Array(x,y,z);

I would be passing values to the variables x,y,z like x0[0] = "test", x0[1] = "another",...
I need to create a variable number of such arrays, so say if i pass a value say 10, it should create 10 different arrays for me. How does one do this, is it allowed to use a loop for creating variables?

There is something called as multi dimensional array, but I wasn't sure of how to use it. I tried but apparently it is very different and I did not understand it. Also, I thought of declaring an object, and passing values to it's parameters, and create objects as needed. Is this possible? And which method will be better?

Is there any other way of doing this? maybe something which would be most efficient?

Naivejs
  • 57
  • 6

3 Answers3

2

Just create an array of arrays:

var myArrays = [];

for(var i=0;i<something;i++){
    myArrays.push([x,y,z]);
}

Then, instead of x0[0] you would have x[0][0].

Note, you shouldn't use new Array but instead use [].

James Montagne
  • 77,516
  • 14
  • 110
  • 130
  • Ok, i just tried this. `var myArrays = []; var x= "a"; var y="b"; var z="c"; for(var i=0;i<4;i++){ myArrays.push([x,y,z]); }` I get x[0][0] as a, which is good. but x[0][1] gives undefined. How do i get b? – Naivejs Sep 09 '11 at 20:32
  • Works for me: http://jsfiddle.net/6YjHX/ `x` is just "a", the arrays is `myArrays`, so `myArrays[0][1]` – James Montagne Sep 09 '11 at 20:35
1

Do you mean

var x  =[];
x[0]=["x","y","z"];
x[1]=["x","y","z"];
x[2]=["x","y","z"];

or

function addArr(arr,newarr) {
  arr[arr.length]=newarr; // or arr.push(newarr);
}
var x  =[];
for (var i=0;i<10;i++) {
  addArr(x,[i,i+1,i+2]);
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1
var n = 100; // number of x-arrays (x0, x1, ...)
var allX = new Array(n); // array of length n

for (var i=0; i<n; i++){
   allX[i] = [x,y,z]; // array with elements x, y, z
}

now you can access e.g. the second array (x1) like:

var x1 = allX[1];

or the third element of the third array (x2[2]) like:

var z = allX[2][2];

Edit: see this explanation about the differences of array declaration between new Array() and []

Community
  • 1
  • 1
Remi
  • 20,619
  • 8
  • 57
  • 41
  • Sorry, I'm a little naive, but it looks like this should solve it. Thanks for the detail on how to use it :) – Naivejs Sep 09 '11 at 20:37
  • You're welcome! Just because you are new here: don't forget to accept an answer and upvote whichever you liked! Good luck with your code. – Remi Sep 09 '11 at 20:39
  • @mplungjan : I checked out the differences. I now let the first declaration use the known number of elements by 'new Array(n)' because it is often [fastest](http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr/3132371#3132371). The elements of sub-arrays are known, rather than the number of elements, so no need to call the Array-constructor and '[]' will be [fastest](http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr/1273936#1273936) and is more readable. Goed zo? – Remi Sep 10 '11 at 15:44
  • Please read the complete post and links in the first link you posted. – mplungjan Sep 11 '11 at 14:14
  • I did (again), and continue to understand that `new Array(..)` can be fastest if you know the number of elements beforehand; otherwise `[]` should be used. What am I seeing wrong here? – Remi Sep 11 '11 at 20:19