SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Wouter's Avatar
    Join Date
    Jan 2002
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question return double array - 2dimensional array???

    Hi

    how can a function return a double array?
    here's some code to explain

    function dummy(param)
    {

    var array0 = new Array();
    var array1 = new Array();

    for (i=0 ; i<5; i++ ){array0[i]=i; array1[i]=4-i;}

    if (param == 0) return(array0);
    if (param == 1) return(array1);

    }

    but I want to return array0 and array1 at the same time

    can I make an array existing out of arrays (2dimensional array) in JavaScript?

    anyone an idea?? Tx

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First of all; why do you want a multi dimensional array?

    Maybe it's a better idea to return an array of objects
    Example:
    http://www.sitepointforums.com/showt...163#post552803

    If you want a 2D array anyway:
    Code:
          function dummy2() 
          { 
            var array0 = new Array(); 
            var array1 = new Array(); 
            for(i = 0 ; i < 5; i++)
            {
              array0[i] = i; 
              array1[i] = 4 - i;
            } 
            var r = new Array();
            r[0] = array0;
            r[1] = array1;
            return r;
          } 
          // Testing...
          var a = dummy2();
          for(var i = 0; i < a.length; i++)
          {
            for(var j = 0; j < a[i].length; j++)
            {
              document.write('a[' + i + '][' + j + '] = ' + a[i][j] + '<br>');
            }
          }

  3. #3
    SitePoint Enthusiast Wouter's Avatar
    Join Date
    Jan 2002
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tx to all

    I will never forget this technique again.
    Happy year-ending


Bookmarks

Posting Permissions

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