javascript 数组长度未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13310566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array length undefined
提问by don
I'm trying to get the length of a multidimensional Array as follows, but when I test it with alert()
I get undefined. I would like to know how many items has the parent array (myArray), as I would like then to use it in a cycle i=0;i<myArray.length;i++
.
我正在尝试按如下方式获取多维数组的长度,但是当我使用它进行测试时,alert()
我得到了未定义的。我想知道有多少项有父数组 (myArray),因为我想在一个循环中使用它i=0;i<myArray.length;i++
。
Any ideas?
有任何想法吗?
myArray = array = {
'def':array = {
"first":"value",
},
0 : array = {
"T":"Some text",
},
1 : array = {
"T":"Some text",
},
};
leng = myArray.length;
alert(leng);
回答by Fábio Santos
You can create an array using array syntax:
您可以使用数组语法创建数组:
array = ["first item", {second:'item'}, 3];
array.def = {something:"else"};
alert(array.length);
And here's how you create a multidimensional array using an array of arrays:
以下是使用数组数组创建多维数组的方法:
array = [
[1,2,3],
[4,5,6],
[7,8,9]
];
alert(array[0][2]); // alerts "3"