Array length undefined [closed]

放肆的年华 提交于 2019-12-04 07:13:16

问题


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++.

Any ideas?

myArray = array = {
        'def':array = {
            "first":"value",
        },
        0 : array = {
            "T":"Some text",
        },
        1 : array = {
            "T":"Some text",
        },
};

leng = myArray.length;
alert(leng);

回答1:


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"


来源:https://stackoverflow.com/questions/13310566/array-length-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!