-3

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);
Dunhamzzz
  • 14,682
  • 4
  • 50
  • 74
don
  • 4,113
  • 13
  • 45
  • 70
  • 4
    You are creating an object not an array.. – OptimusCrime Nov 09 '12 at 15:10
  • 2
    See [Length of Javascript Object (ie. Associative Array)](http://stackoverflow.com/questions/5223/length-of-javascript-object-ie-associative-array) for deriving the length of an object/associative – Alex K. Nov 09 '12 at 15:11
  • I don't see any Array in your code. Your `myArray` *object* does not have a `length` property – Bergi Nov 09 '12 at 15:11
  • That's not an object either, well it is but it doesn't contain the values you expect. – James Nov 09 '12 at 15:14

1 Answers1

3

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"
Fábio Santos
  • 3,899
  • 1
  • 26
  • 31