3d array in javascript

Discussion in 'Javascript' started by hardik, Apr 18, 2006.

  1. hardik

    hardik Guest

    how i can set 3*3 array in javascript i have tried this but didnt work

    <Script>
    var a[2][2][2]=new array()
    <\Script>


    but it didnt work.


    Bye & Best Of Luck.
     
    hardik, Apr 18, 2006
    #1
    1. Advertising

  2. hardik

    Hal Rosser Guest

    "hardik" <> wrote in message
    news:...
    > how i can set 3*3 array in javascript i have tried this but didnt work
    >
    > <Script>
    > var a[2][2][2]=new array()
    > <\Script>
    >
    >
    > but it didnt work.
    >


    var a = new Array();
    a[0]= new Array();
    a[0][5] = new Array;
    a[0][5][72]="<h3>Hello matey</h3>";
    document.writeln(a[0][5][72]);
     
    Hal Rosser, Apr 18, 2006
    #2
    1. Advertising

  3. hardik

    Hal Rosser Guest

    "Hal Rosser" <> wrote in message
    news:Nz_0g.31359$...
    >
    > "hardik" <> wrote in message
    > news:...
    > > how i can set 3*3 array in javascript i have tried this but didnt work
    > >
    > > <Script>
    > > var a[2][2][2]=new array()
    > > <\Script>
    > >
    > >
    > > but it didnt work.
    > >

    >
    > var a = new Array();
    > a[0]= new Array();
    > a[0][5] = new Array;
    > a[0][5][72]="<h3>Hello matey</h3>";
    > document.writeln(a[0][5][72]);


    You could loop through each array and declare new arrays for each element,
    also
    This is kinda clumsy - so I'm sure others will post a better solution.-
    where I will take notes.
    But the point is: its really an array of arrays [of arrays].... rather than
    a single multidimensional array.

    >
    >
     
    Hal Rosser, Apr 18, 2006
    #3
  4. hardik

    RobG Guest

    hardik said on 18/04/2006 2:34 PM AEST:
    > how i can set 3*3 array in javascript i have tried this but didnt work
    >
    > <Script>
    > var a[2][2][2]=new array()
    > <\Script>


    If you already had an array called 'a' with an array at index 2 and
    another array at that array's index 2, then you could create an array at
    index 2 of that last array.

    But you haven't, so you have a script error. Also, the built-in array
    object has a capital 'A' (to signify that you can use it as a
    constructor perhaps).

    To save on typing and potential typos, use an initialiser:

    var a = [];
    a[2] = [];
    a[2][2] = [];
    a[2][2][2] = [];


    A one dimension array 1x3:

    var a = ['A', 'B', 'C'];


    A two dimension array 2x3:

    var a = [
    ['A', 'B', 'C'],
    ['D', 'E', 'F']
    ];


    A three dimension array 2x3x3:

    var a = [
    [
    ['a','b','c'],
    ['d','d','f'],
    ['g','h','i']
    ],
    [
    ['j','k','l'],
    ['m','n','o'],
    ['p','q','r']
    ]
    ]

    alert( a[0][1][2] ); // shows f


    Keep going and it gets much harder to read...



    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/FAQ>
     
    RobG, Apr 18, 2006
    #4
  5. hardik

    VK Guest

    hardik wrote:
    > how i can set 3*3 array in javascript i have tried this but didnt work
    >
    > <Script>
    > var a[2][2][2]=new array()
    > <\Script>


    See the samples of 2D and 3D arrays emulation at
    <http://www.geocities.com/schools_ring/ArrayAndHash.html>


    :)
     
    VK, Apr 18, 2006
    #5
  6. hardik

    hardik Guest

    thank u friends
     
    hardik, Apr 18, 2006
    #6
  7. hardik

    Randy Webb Guest

    RobG said the following on 4/18/2006 1:57 AM:

    <snip>

    > A two dimension array 2x3:
    > var a = [
    > ['A', 'B', 'C'],
    > ['D', 'E', 'F']
    > ];


    I think that Rob knows, without reading further, what this post says but
    a is not a "two dimension array" as it is a simple array that has arrays
    as members. Javascript arrays are linear in fashion and as such you
    can't have multi-dimensional arrays.

    Sorry Rob, but I had to post for posterity sake :)

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Apr 18, 2006
    #7
  8. hardik

    RobG Guest

    Randy Webb wrote:
    > RobG said the following on 4/18/2006 1:57 AM:
    >
    > <snip>
    >
    >> A two dimension array 2x3:
    >> var a = [
    >> ['A', 'B', 'C'],
    >> ['D', 'E', 'F']
    >> ];

    >
    >
    > I think that Rob knows, without reading further, what this post says but
    > a is not a "two dimension array" as it is a simple array that has arrays
    > as members. Javascript arrays are linear in fashion and as such you
    > can't have multi-dimensional arrays.
    >
    > Sorry Rob, but I had to post for posterity sake :)


    That's fine. Others may say that a is a 2D array (matrix) that is
    constructed using 2 one-dimensional JavaScript Array objects. :)


    --
    Rob
     
    RobG, Apr 18, 2006
    #8
  9. hardik

    Randy Webb Guest

    RobG said the following on 4/18/2006 9:31 AM:
    > Randy Webb wrote:
    >> RobG said the following on 4/18/2006 1:57 AM:
    >>
    >> <snip>
    >>
    >>> A two dimension array 2x3:
    >>> var a = [
    >>> ['A', 'B', 'C'],
    >>> ['D', 'E', 'F']
    >>> ];

    >>
    >>
    >> I think that Rob knows, without reading further, what this post says
    >> but a is not a "two dimension array" as it is a simple array that has
    >> arrays as members. Javascript arrays are linear in fashion and as such
    >> you can't have multi-dimensional arrays.
    >>
    >> Sorry Rob, but I had to post for posterity sake :)

    >
    > That's fine. Others may say that a is a 2D array (matrix) that is
    > constructed using 2 one-dimensional JavaScript Array objects. :)
    >


    Tis true, but VK might say that "RobG says JS has a multi-dimensional
    array" <shudder> <g>

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Light humor is good for the soul every now and then.
     
    Randy Webb, Apr 18, 2006
    #9
  10. hardik

    VK Guest

    Randy Webb wrote:
    > Tis true, but VK might say that "RobG says JS has a multi-dimensional
    > array" <shudder> <g>


    I believe in the JavaScript Array being Dynamic, Sparse, Jagged and
    now, and ever and forever!

    :-D

    P.S. Please, it is just a joke, not a call for discussion.
     
    VK, Apr 18, 2006
    #10
  11. hardik

    Randy Webb Guest

    VK said the following on 4/18/2006 1:37 PM:
    > Randy Webb wrote:
    >> Tis true, but VK might say that "RobG says JS has a multi-dimensional
    >> array" <shudder> <g>

    >
    > I believe in the JavaScript Array being Dynamic, Sparse, Jagged and
    > now, and ever and forever!


    <g>

    > :-D
    >
    > P.S. Please, it is just a joke, not a call for discussion.


    Surprisingly enough, I took it that way :)


    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Apr 19, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    505
    David RF
    Sep 3, 2009
  2. Tom
    Replies:
    3
    Views:
    218
    salsablr
    Dec 20, 2004
  3. Tuan  Bui
    Replies:
    14
    Views:
    492
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  4. VK
    Replies:
    36
    Views:
    664
    Martin Honnen
    Aug 3, 2005
  5. BrianP
    Replies:
    2
    Views:
    292
Loading...

Share This Page