What's wrong with Multidimensional Array?

Discussion in 'Javascript' started by Dominik, Feb 19, 2012.

  1. Dominik

    Dominik Guest

    --- Example 1
    var myarray=[[],[]];
    myarray[0][1]="NA"
    print(myarray[0][1]); // output "NA"

    --- Example 2
    var myarray=[[],[]];
    for (var x = 0; x < 5; x++) {
    for (var y = 0; y < 5; y++) {
    myarray[x][y]=0;
    }
    }

    for (var x = 0; x < 5; x++)
    print(myarray[x]); // output error myarray[x] is undefined !?!

    What's wrong with example 2 ????
     
    Dominik, Feb 19, 2012
    #1
    1. Advertisements

  2. Dominik

    Dominik Guest

    On Sun, 19 Feb 2012 12:52:00 +0100, Stefan Weiss <>
    wrote:

    >On 2012-02-19 12:37, Dominik wrote:
    >> var myarray=[[],[]];

    >
    >myarray now has (empty) arrays at index 0 and 1.


    --- Example 1.1
    var myarray=[[],[]];
    myarray[10][10]="NA"
    print(myarray[10][10]); // output error myarray[10] is undefined

    OMG What a mistake-a to make-a. :)
    Thank you!

    [snip]
    >At the third iteration, you're trying to set myarray[2][0] = 0.
    >myarray[2] is undefined, so you get an error.
    >
    >You could create the child arrays when you need them, instead of doing
    >it in advance:


    OK, I see it now.
    But I'm curius, is it possible to create empty multidimensional array in
    advance?
     
    Dominik, Feb 19, 2012
    #2
    1. Advertisements

  3. Dominik

    Dominik Guest

    On Sun, 19 Feb 2012 14:16:08 +0100, Stefan Weiss <>
    wrote:

    >On 2012-02-19 13:32, Dominik wrote:
    >> But I'm curius, is it possible to create empty multidimensional array in
    >> advance?

    >
    >Well, strictly speaking there's no such thing as a multidimensional
    >array in JS. There are only arrays of arrays. The two are often used in
    >a similar way, but there are differences. One of them is that there's no
    >way to initialize the child arrays with a simple statement*.


    Thank you very much for your help. :)
     
    Dominik, Feb 19, 2012
    #3
  4. Stefan Weiss wrote:

    > In JS 1.7, you might use array comprehension to make the initialization
    > more concise:
    >
    > var myarray = [[] for each (i in range(0, 4))];
    > // assuming a suitable range() generator function
    >
    > Not very portable, though. If you need such arrays a lot, you may want
    > to define a function to handle the initializations.


    Given that, according to my current research, Array comprehension is *only*
    supported in (Mozilla) JavaScript 1.7 and later, I do not think it deserves
    the attribute "portable" at all.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Feb 20, 2012
    #4
  5. Dominik

    Evertjan. Guest

    Thomas 'PointedEars' Lahn wrote on 20 feb 2012 in comp.lang.javascript:

    > Given that, according to my current research, Array comprehension is
    > *only* supported in (Mozilla) JavaScript 1.7 and later, I do not think
    > it deserves the attribute "portable" at all.


    I fail to comprehend the meaning of such atributable comprehension, Thomas.

    Could you divulge?

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Feb 20, 2012
    #5
  6. In comp.lang.javascript message <irp1k7puv7pgmuhuuhnpovq6dolg7pubea@4ax.
    com>, Sun, 19 Feb 2012 13:32:41, Dominik <> posted:

    >
    >OK, I see it now.
    >But I'm curius, is it possible to create empty multidimensional array in
    >advance?


    One can assign a value to a variable which does not already exist.

    For counting purposes, it would be useful to be able to increment a
    variable which currently is value=undefined or is non-existent, with the
    pre-increment value taken as either 0 or "", depending on the nature of
    the increment. Possibly with X+++ and X ++= 7.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike 6.05 WinXP.
    Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
    Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
    DOS/Win/UNIX now 2.0.6; see <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.
     
    Dr J R Stockton, Feb 20, 2012
    #6
  7. Dominik

    Scott Sauyet Guest

    Evertjan wrote:
    > Thomas 'PointedEars' Lahn wrote on 20 feb 2012 in comp.lang.javascript:
    >
    >> Given that, according to my current research, Array comprehension is
    >> *only* supported in (Mozilla) JavaScript 1.7 and later, I do not think
    >> it deserves the attribute "portable" at all.

    >
    > I fail to comprehend the meaning of such atributable comprehension, Thomas.


    <https://developer.mozilla.org/en/JavaScript/Guide/
    Predefined_Core_Objects#Array_comprehensions>

    -- Scott
     
    Scott Sauyet, Feb 20, 2012
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.