如何获得多维数组的长度

时间:2014-03-22 08:40:07

标签: javascript jquery multidimensional-array

我有一个数组如下:

$('#MMTable').data('arMSItems').push([[],[],[]]);
$('#MMTable').data('arMSItems')[0][0]['ProductNR'] = 1;
$('#MMTable').data('arMSItems')[0][0]['Title'] = 1;
$('#MMTable').data('arMSItems')[0][0]['Description'] = 1;
$('#MMTable').data('arMSItems').push([[],[],[]]);

//-------------------------------------------------
$('#MMTable').data('arMSItems')[1][0]['ProductNR'] = 1;
$('#MMTable').data('arMSItems')[1][0]['Title'] = 1;
$('#MMTable').data('arMSItems')[1][0]['Description'] = 1;
$('#MMTable').data('arMSItems').push([[],[],[]]);

//-------------------------------------------------
$('#MMTable').data('arMSItems')[2][0]['ProductNR'] = 1;
$('#MMTable').data('arMSItems')[2][0]['Title'] = 1;
$('#MMTable').data('arMSItems')[2][0]['Description'] = 1;

alert( $('#MMTable').data('arMSItems')[0][0].length ); 
// Results --> 3

//-------------------------------------------------
$('#MMTable').data('arMSItems').push([[],[],[]]);
$('#MMTable').data('arMSItems')[0][1]['ProductNR'] = 1;
$('#MMTable').data('arMSItems')[0][1]['Title'] = 1;
$('#MMTable').data('arMSItems')[0][1]['Description'] = 1;
$('#MMTable').data('arMSItems').push([[],[],[]]);

//-------------------------------------------------
$('#MMTable').data('arMSItems')[1][1]['ProductNR'] = 1;
$('#MMTable').data('arMSItems')[1][1]['Title'] = 1;
$('#MMTable').data('arMSItems')[1][1]['Description'] = 1;

alert( $('#MMTable').data('arMSItems').length );
// Results --> 5

//----------------------------------------------------------------

如何获取第二个字段中的元素数量? 例如。 :

[0][1]['ProductNR']
[1][1]['ProductNR']

我希望结果为2元素。但我怎么能得到它?

E.g。第二个字段是" 1"如下:[x] [1] [x]

alert( $('#MMTable').data('arMSItems')[x][1][x].length );

数组包含多少个元素(我希望结果为2)?

非常感谢提前。

1 个答案:

答案 0 :(得分:1)

逻辑上你的数组应该是,

['arMSItems']['ProductNR'][0]
['arMSItems']['Title'][0]
['arMSItems']['Description'][0]

['arMSItems']['ProductNR'][1]
['arMSItems']['Title'][1]
['arMSItems']['Description'][1]
// so on..

然后你可以使用

获得长度
alert(arr['arMSItems']['ProductNR'].length);

<强> Demo