www.webdeveloper.com
Results 1 to 10 of 10

Thread: 2D arrays in JavaScrpit?

Hybrid View

  1. #1
    Join Date
    Jan 2006
    Posts
    1

    2D arrays in JavaScrpit? [SOLVED]

    I am writing a program in which it would be very useful to use a 2D array, but I have not been able to find code for a 2D array, and I'm starting to doubt that they exist in JavaScript. If they don't, what is the easiest way to script one? An array of arrays seems rather large and unwieldy...
    Thanks, PW

    Update: Figured it out, apparently the book i was using isn't current...
    Last edited by Poliwop; 01-07-2006 at 02:36 PM.

  2. #2
    Join Date
    Jan 2005
    Posts
    3,067
    First you create and array, then you create the first element as an array, and then insert two elements into it:
    Code:
    var myarray = new Array();
    myarray[0] = new Array(2); // Make the first element an array of two elements
    myarray[0,0] = 'first'; //populate the first part of the first element of myarray
    myarray[0,1] = 'second'; //populate the second part of the first element of myarray

  3. #3
    Join Date
    Jul 2007
    Posts
    1

    Thumbs down javascript 2d arrays are a sham

    From my experience there are no 2D arrays. The above code works, but once you try to add a second element to the array, the first one is lost. Eg:

    Code:
    var myarray = new Array();
    myarray[0] = new Array(2); // Make the first element an array of two elements
    myarray[0,0] = 'first'; //populate the first part of the first element of myarray
    myarray[0,1] = 'second'; //populate the second part of the first element of myarray
    
    myarray[1] = new Array(2);
    myarray[1,0] = 'santa';
    myarray[1,1] = 'claus';
    
    
    alert("myarray[0,0] should be 'first' but it is: " + myarray[0,0]);	//displays 'santa'  
    alert("myarray[0,1] should be 'second' but it is: " + myarray[0,1]);	//displays 'claus'
    I just created 2 arrays, instead.

  4. #4
    Join Date
    Jun 2007
    Posts
    347
    var my2D =
    ["first","santa"]
    ,["second","claus"]
    ];

    alert(my2D = my2D[0][0])


    Whether 2D arrays exist or not is a matter of opinion, I suppose. A Javascript 2D array can be "ragged". But then again JS arrays can't be compared to arrays in say Java anyway.

  5. #5
    Join Date
    Dec 2007
    Posts
    1
    Quote Originally Posted by jamiek
    From my experience there are no 2D arrays. The above code works, but once you try to add a second element to the array, the first one is lost. Eg:

    Code:
    var myarray = new Array();
    myarray[0] = new Array(2); // Make the first element an array of two elements
    myarray[0,0] = 'first'; //populate the first part of the first element of myarray
    myarray[0,1] = 'second'; //populate the second part of the first element of myarray
    
    myarray[1] = new Array(2);
    myarray[1,0] = 'santa';
    myarray[1,1] = 'claus';
    
    
    alert("myarray[0,0] should be 'first' but it is: " + myarray[0,0]);	//displays 'santa'  
    alert("myarray[0,1] should be 'second' but it is: " + myarray[0,1]);	//displays 'claus'
    I just created 2 arrays, instead.
    I think for example [1][0] should be used instead of [1,0]

    Code:
    var myarray = new Array();
    myarray[0] = new Array(2); // Make the first element an array of two elements
    myarray[0][0] = 'first'; //populate the first part of the first element of myarray
    myarray[0][1] = 'second'; //populate the second part of the first element of myarray
    
    myarray[1] = new Array(2);
    myarray[1][0] = 'santa';
    myarray[1][1] = 'claus';
    
    
    alert(myarray[0][0]);	//displays 'first'  
    alert(myarray[0][1]);	//displays 'second'
    (This is an array of arrays though; it can be jagged, not really that kind of "rectangular" 2D arrays)

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    As Banana Ananda very well noticed, on using JSON you shorten the code as you don't need to write the indexes:
    Code:
    var myarray=[
    ['first','second'],
    ['santa','claus']
    ]

  7. #7
    Join Date
    Feb 2008
    Posts
    2

    The Answer

    I just noticed too, when i was trying to implement a 2D array into JS. It is Screwed. But here is a solution if you want to stick to multidimensional arrays: An Object.

    function Array2D() {
    function setArray(length) { // set an array inside the object
    this.elem=new Array();
    for(i=0;i<length;i++) {
    this.elem[i]=null;
    }
    }
    this.setArray=setArray;
    this.elem=null; // our array
    }

    With this little fella you can hide your intentions from the Evil Javascript Developers, and secretly make a 2D array:

    arr = new Array();
    arr[0] = new Array2D();
    arr[0].setArray(2);
    arr[0].elem[0] = "Hy, ";
    arr[0].elem[1] = "I'm ";
    arr[1] = new Array2D();
    arr[1].setArray(2);
    arr[1].elem[0] = "Da";
    arr[1].elem[1] = "vid!";

    alert(arr[0].elem[0] + arr[0].elem[1] + arr[1].elem[0] + arr[1].elem[1]); // this will say "Hy, I'm David!"
    Last edited by DavidAdama; 02-01-2008 at 01:12 PM.

  8. #8
    Join Date
    Jan 2010
    Posts
    1
    hi im new to javascript but I just made a 2D array that works:

    Code:
    function processData() {
    	var data = new Array();
    	for(var c=0; c<document.forms.length; c++) {
    		data[c] = new Array();
    		data[c][0] = document.forms[c].elements['caption'].value;
    		data[c][1] = document.forms[c].elements['source'].value;
    		data[c][2] = document.forms[c].elements['info'].value;
    	}
    	alert(data.join('++'));
    }

  9. #9
    Join Date
    Feb 2008
    Posts
    2
    Or that
    I might have complicated it a bit:S

  10. #10
    Join Date
    May 2012
    Posts
    1

    Smile 2D Array in Javascript

    Hi everyone, I just found this forum and read this thread. I realized to post my idea on how to implement it:

    *********************
    Declare 2D Array in Javascript:
    var myArray = new Array(10,5);

    This is like int myArray[10][5]; for other languages.


    *********************
    Access 2D Array in Javascript:
    ex.: myArray[1,3] = 777;

    This is like int myArray[1][3]=777; for other languages.

    *********************

    Hope this could help. Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles