Skip to content
Advertisement

How to get the number of columns of a 2dimension array in javascript?

I have a 2 dimensional array albumPhotos[x][y]. The rows are photoalbums and the columns are links to photos.

Now every photoalbum has a different number of photos , that means that every row in this array has a different number of columns.

I am trying to check whats the length of every row in this array , that means how many columns every row has. How can i do that in javascript?

I tried :

for(var i=0; i< numberOfRows ; i++)
    for(var x=0; x < albumPhotos[i].length; x++) ...

but apparently this is wrong command in javascript. Then i tried something like this :

for(var i=0; i< numberOfRows ; i++)
    for(var x=0; x < albumPhotos.rows[i].cells.length; x++)

but it seems wrong again. I think this is for html tables and not for arrays.

Any ideas?

Advertisement

Answer

You just need to check the .length of the current row.

var numberOfRows = albumPhotos.length;

for(var i=0; i < numberOfRows ; i++)
    console.log(albumPhotos[i].length);

And you’re right, the second example is for table elements, not Arrays.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement