Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

arrays

Joe2003Joe2003 Member Posts: 92
can you use a 2d array in javascript????

if so how do you define it?

Comments

  • CyGuyCyGuy Member Posts: 312
    : can you use a 2d array in javascript????
    :
    : if so how do you define it?
    :
    [green]sure you can look:
    [code]var array2deep = [[1,2,3],[2,3,4],[4,5,6]];[/code]
    to access the last element do this:
    [code]x = array2deep[3][3][/code]
    there you have it[/green]

    [green]WHAT you;see_is_what=you.get;[/green]
    -Russ aka DangeRuss

  • NCS_OneNCS_One Member Posts: 170
    : : can you use a 2d array in javascript????
    : :
    : : if so how do you define it?
    : :
    : [green]sure you can look:
    : [code]var array2deep = [[1,2,3],[2,3,4],[4,5,6]];[/code]
    : to access the last element do this:
    : [code]x = array2deep[3][3][/code]
    : there you have it[/green]
    :
    : [green]WHAT you;see_is_what=you.get;[/green]
    : -Russ aka DangeRuss
    :
    :

    Hi

    Thats a static array, is there any way of doing it but with
    dynamic arrays ???
  • CyGuyCyGuy Member Posts: 312
    [b][red]This message was edited by CyGuy at 2005-12-1 16:27:21[/red][/b][hr]
    : : : can you use a 2d array in javascript????
    : : :
    : : : if so how do you define it?
    : : :
    : : [green]sure you can look:
    : : [code]var array2deep = [[1,2,3],[2,3,4],[4,5,6]];[/code]
    : : to access the last element do this:
    : : [code]x = array2deep[3][3][/code]
    : : there you have it[/green]
    : :
    : : [green]WHAT you;see_is_what=you.get;[/green]
    : : -Russ aka DangeRuss
    : :
    : :
    :
    : Hi
    :
    : Thats a static array, is there any way of doing it but with
    : dynamic arrays ???
    :
    [green]there may be another way to do this, but this is what I have been doing.
    [code]

    /* initialize */
    var row = new Array();
    function ROW()
    { this.column = new Array();}
    for (x=0; x<10; x++)
    {
    row[x] = new ROW();
    }

    /* assignment */
    for (x=0; x<10; x++)
    {
    for (y=0; y<10; y++)
    {
    row[x].column[y] = x * y;
    }
    }

    /* output */
    document.write("<table align = 'center'");
    document.write("<tr>");
    for (x=0; x<10; x++)
    {
    document.write("<td width = '8%'>" + x + "");
    }
    document.write("");

    for (x=0; x<10; x++)
    {
    document.write("<tr>"+x+"");
    for (y=0; y<10; y++)
    {
    document.write("<td>" + row[x].column[y] + "");
    }
    document.write("");
    }

    [/code]
    [/green]
    looks messy, but it has worked for me. the function is a class, and I had to use the OOP to make it work. I wish I knew a better way... hope this helps.


    [green]WHAT you;see_is_what=you.get;[/green]
    -Russ aka DangeRuss



  • BodkinBodkin Member Posts: 100
    [CODE][SIZE=2]
    function make2DArray(x, y) {
    s = new Array(x);
    for(p=0;p<x;p++) {
    s[p] = new Array(y);
    for(q=0;q<y;q++)
    s[p][q] = ""+p+q;
    }
    return s;
    }
    s = make2DArray(4,3);
    alert(s + "
    " + s[s.length-1][s[0].length-1]);
    [/SIZE][/CODE]
Sign In or Register to comment.