Create a multidimensional array

Unlike PHP where you can just say $array[1][5] = "blah"; without declaring the indexes first, in JavaScript, you must declare each array.

As in the example below, we first declare the a_array array (I prefix any array with a_) then loop over the rows and the columns. For each new row (i) we declare that array, then we populate the i j ordered pair with null and then return the array.

Note, these arrays must be rectangles or squares. Every entry needs to be set to null to start with.

function mkmultarray(nrow, ncol)
{
        var a_array = new Array();
        for(i=0; i<=nrow; i++)
        {
                a_array[i] = new Array();
                for(j=0; j<=ncol; j++)
                {
                        a_array[i][j] = null;
                }
        }
        return a_array;
}

Page Comments (Click to edit)






[Click to add or edit comments])

Please prepend comments below including a date

Design by N.Design Studio, adapted by solidGone.org (version 1.0.0)
Have a nice day.