I would have thought that a function to Create multidimensional JavaScript arrays of any size would be very handy. But I have not found one. So I created my own.
Would using array.map work in this case? If so I imagine it would be less 'logical' looking.
//like the BASIC DIM :) use: var multiDimensionalArray = DIM([7,8,9])
function DIM(arrayOfDimensions) {
let localArrayOfDimensions = arrayOfDimensions.slice(); //must copy array as scope is global for arrayofdimentions
let returnArray = [];
if (localArrayOfDimensions.length === 0) {
return undefined; //no more dimensions
}
let arraySize = localArrayOfDimensions.shift(); // size of current dimension
for (let arrayPointer = 0 ; arrayPointer < arraySize ; arrayPointer++){
//for each array pointer in this array dimension, recusivly call DIM, and return and assign the value
returnArray[arrayPointer] = DIM( localArrayOfDimensions );
}
return returnArray; //return here if there are still more array dimensions
}
var test = DIM([5,3,2]);
test[4][2][1] = 'aaaasss';
var t = test[4][2][1];