i know how to create a one dimensional empty array like this:
var data = [];
var length = 100;
console.log(data);
for(var i = 0; i < length; i++) {
data.push(' ');
}
but how do i make the same thing, but two dimensional?
i know how to create a one dimensional empty array like this:
var data = [];
var length = 100;
console.log(data);
for(var i = 0; i < length; i++) {
data.push(' ');
}
but how do i make the same thing, but two dimensional?
Your data
is not an empty array, it is an array of 100 blanks/spaces.
If that's what you mean, then:
var length=100;
var data=[];
for(var i=0; i<length; i++) {
var innerData=[];
for(var j=0; j<length; j++) {
innerData.push(' ');
};
data.push(innerData);
}
I guess what you need is an array of arrays in that case you just fill the outer array with the number of arrays you want, you could use the for loop
var data = [];
var length = 100;
for(var i = 0; i < length; i++) {
data.push([]);
}
console.log(data)
or use fill instead as it's simpler, but the difference is all the arrays created will have same reference as the first array
array=Array(10).fill([])
console.log(array)