3

How do you create a multi dimensional array in Javascript using a for loop ?

1
  • Have you tried anything? Commented Dec 8, 2011 at 7:28

4 Answers 4

13
var test = [];
for(var i = 0; i < 100; i++){
    test.push([i, "lol"]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Simple and works fine.
4
var sDataArray = MultiDimensionalArray(2, 2);

function MultiDimensionalArray(iRows, iCols) {
    var i;
    var j;
    var table = new Array(iRows);

    for (i = 0; i < iRows; i++) {

        table[i] = new Array(iCols);

        for (j = 0; j < iCols; j++) {
            table[i][j] = "";
        }
    }
    return (table);
} 

1 Comment

It looks like this function won't work when initializing arrays with more than 2 dimensions.
2
var arr = [];
for(var i = 0;i<100;++i){
  arr[i] = [];
  for(var j = 0; j < 100; ++j){
    arr[i][j] = i*j;
  }
}

Comments

0

Its an array of arrays.

var arr = [

    [0,1,2],
    [3,4,5],
    [
        ['a','b','c']
    ]

];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.