0

I want to create a multidimensional array like this:

array[0][1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
array[1][21,22,23,24,25,26,27....]
array[.][....]

How can I do this in Javascript?

I have tried this:

var squares = new Array();
        for(var i = 1; i <= 8; i++)
        {
            for(var j = 1; j <= 20; j++)
            {
                squares.push(i, j);     
            }

        }

How can I accomplish this?

1
  • Updated with the answer. Commented Sep 1, 2014 at 14:30

4 Answers 4

3

You can do something like this:

var squares = new Array();
for(var i = 0; i <= 8; i++)
{
    squares[i] = new Array();
    for(var j = (i * 20) + 1; j <= 20 * i + 20; j++)
        if (squares[i] == null)
            squares[i] = j;
        else
            squares[i].push(j);
}

Output comes like:

array[0][1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
array[1][21,22,23,24,25,26,27....]
Sign up to request clarification or add additional context in comments.

3 Comments

I have already test that, and I get this error: Error: squares[i] is undefined
@user500468 Check now! :)
@user500468 Check the udpated answer. Was my mistake. Sorry!
0
var array = []; // Main array
var numArrays = 10, // Number of sub-arrays
    numPerArray = 20; // Number of squares per sub-array

for(var i = 0; i < numArrays; i++){
    var subArray = [];
    // Number to start at
    var start = i * numPerArray;
    // Count up to start + numPerArray
    for(var j = start; j < start + numPerArray; j++){
        subArray.push(j);
    }
    // Add to main array
    array.push(subArray);
}

Comments

0

Use modulus operand to limit the inner array's size

var limit = 80
var inner_limit = 20
var square=[]
var inner =[]
for(var i=1;i<=limit;i++){
    inner.push(i)        
    if(i%inner_limit==0){
        square.push(inner)
        inner = []
    }

}

6 Comments

Doesn't give the expected output! :(
fix it, can you try again?
Good. Works now... Too complicated anyways! :)
@PraveenKumar: This example works better than yours. In your example, array[1] contains 11, 12, 13 etcetc..
@user500468 how can I make mine better, as this is slightly complicated. Yea, and that's what you wanted right? Oops, is it so, I will fix it right away..
|
0

You can do it with two "for" loops. In the first loop you go through the main array and for each element add the elements from the second loop.

var arrayLength = 10; // Main array length
var limit = 20; // Number of squares
var array = [];

for ( var i = 0; i < arrayLength; i++ )
{
    array[i] = []; // Create subArray

    for( var j = 1; j <= limit; j++ )
    {
        array[i].push(j);
    }
}

1 Comment

You may want to add some sort of explanation about what your answer does/is, some context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.