0

I have a one dimensional array which I want to convert to two dimensional array. So, I use loop for that. To prove that every index array has been correctly entered, I print it to the screen. But it only prints the first row and after that it throws an error

Uncaught TypeError: Cannot set property '0' of undefined

I heard that in JS the array is somewhat different compared to other languages (I'm most familiar with Java). So, maybe I have confused it somehow, but I have no idea.

Here's my code.

var k = 0;
//move tha matrix from one dimensional matrix to two dimensional
var cubes = new Array([]);
for(var i = 0; i < n; i++){
 for(var j = 0; j <= n; j++){
  cubes[i][j]=matriks[k];
  document.write("["+ i +"]["+ j +"] = "+cubes[i][j].value + " ");
  k++;
 }
}

The matrix is a one dimensional array. And the cubes is the two dimensional array which I want to place the matrix into.

N.Koptsov
  • 3
  • 3
catris25
  • 1,173
  • 3
  • 20
  • 40
  • in cubes first row and second row what must be replace?in first row mateix elements and in second row empty? – Sarkhan May 22 '15 at 06:36
  • possible duplicate of [How to convert simple array into two-dimensional array(matrix) in javascript (or jquery)](http://stackoverflow.com/questions/4492385/how-to-convert-simple-array-into-two-dimensional-arraymatrix-in-javascript-or) – AlvaroAV May 22 '15 at 06:37

3 Answers3

0

The problem is that you do not tell JavaScript that the Objects stored in the outer-most array should be an Array. When you tried to use the inner arrays, you had not yet initialized them, so you were getting undefined. To fix this, you can simply change your code to:

var k=0; 
var cubes = [];
var i, j;
for(i=0; i<n; i++){
    cubes[i] = [];
    for(j=0; j<=n; j++){
        cubes[i][j]=matriks[k];
        document.write("["+i+"]["+j+"] = "+cubes[i][j].value+" ");
        k++;
    }
    j=0;
}
Kiith Nabaal
  • 366
  • 2
  • 5
  • 16
  • 1
    *"The problem is that you do not tell JavaScript what kind of Object the outer-most array should be storing"* Huh? JavaScript is loosely-typed (usually, it does [now] haev `Int32Array` and such). The code above is correct, but the explanation doesn't really make a lot of sense. – T.J. Crowder May 22 '15 at 06:42
  • What I am referring to is that you can't just use a variable as if it were an array unless it is an array type, right? – Kiith Nabaal May 22 '15 at 06:43
  • The OP's `cubes` **is** an array. So is the first entry in it. – T.J. Crowder May 22 '15 at 07:00
  • Right, but the rest of the entries weren't an array. Sure I wasn't very clear, but my point was that you can't do arr[2][0], unless arr[2] stores an Array. He was trying to access elements of an array that didn't exist. He had to specify that each index of cubes stores an array, which I put in my answer. – Kiith Nabaal May 22 '15 at 07:24
  • Thanks for the response, everyone! It works now. But I have another confusion. So, each time I want to access the array, I have to use '.value' in it. Once I tried without the .value, it returns an undefined element or something like that. – catris25 May 22 '15 at 07:29
  • 1
    @starlightsie: That will depend entirely on what's in the original matrix. – T.J. Crowder May 22 '15 at 07:41
  • okay, thanks! It seems like I always have to use .value then. – catris25 May 22 '15 at 07:46
0

the declaration of the bidimensional array "cubes" is not correct as you do, you have to initialize each row of your matrix, this generate the error

var k=0;
//move tha matrix from one dimensional matrix to two dimensional
var cubes = [];
for(var i=0; i<n; i++){
    cubes[i] = [];
    for(var j =0; j<=n; j++){
        cubes[i][j]=matriks[k];
        console.log("value at ", i, j, " = ", cubes[i][j]);
        k++;
    }
}
Simone Sanfratello
  • 1,520
  • 1
  • 10
  • 21
0

This line:

var cubes = new Array([]);

creates an array with one entry in it, which is another array. It does not create a multi-dimensional array (JavaScript doesn't have them, it has arrays of arrays). This means that there is no array anywhere in cubes except at entry [0].

To fix it, change that to

var cubes = [];

and add

cubes[i] = [];

just after

for(var i=0; i<n; i++){

to create each subarray.

E.g.:

var k=0;
//move tha matrix from one dimensional matrix to two dimensional
var cubes = [];
for(var i=0; i<n; i++){
    cubes[i] = [];
    for(var j =0; j<=n; j++){
        cubes[i][j]=matriks[k];
        document.write("["+i+"]["+j+"] = "+cubes[i][j].value+" ");
        k++;
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875