最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 mand 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?

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 mand 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?

Share Improve this question edited Jun 26, 2013 at 8:47 Johny Jaz asked Jun 25, 2013 at 16:10 Johny JazJohny Jaz 8853 gold badges14 silver badges26 bronze badges 7
  • Why aren't you just adding albumPhotos[i].length to a variable in each iteration of your outer loop? – Bojangles Commented Jun 25, 2013 at 16:13
  • I dont see how that would solve the problem.. I get an error when i use this in the for loop.Would it change if i used a variable before it? Is this correct syntax? – Johny Jaz Commented Jun 25, 2013 at 16:14
  • Uncaught TypeError: Cannot read property 'length' of undefined – Johny Jaz Commented Jun 25, 2013 at 16:15
  • Do a console.log(albumPhotos) and see what's in it – Bojangles Commented Jun 25, 2013 at 16:16
  • 3 What exactly is numberOfRows? Why don't you use ; i < albumPhotos.length;? – Ian Commented Jun 25, 2013 at 16:16
 |  Show 2 more ments

2 Answers 2

Reset to default 3

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.

let x = [
    [2,3,4],
    [1,1,1]
];
console.log(x.length)  // row count: 2
console.log(x[0].length) // column count: 3

just when u have same size for every row, since your guide is the first row to get your numbers of columns

发布评论

评论列表(0)

  1. 暂无评论