Free Tarot Readings

JavaScript JS : Create a multidimensional array of any size easily : and assign a fill value


JavaScript JS : Create a multidimensional array of any size easily : and assign a fill value

//like the BASIC DIM 馃檪 use: var multiDimensionalArray = DIM([7,8,9] , fill value if desired)
function DIM( arrayOfDimensions , theValue)
{
let localArrayOfDimensions = arrayOfDimensions.slice(); //must copy array as scope is global for arrayofdimensions
let returnArray = [];
if (localArrayOfDimensions.length === 0) //no more dimensions
{
return theValue; //for variable only: string, number, Boolean
//return JSON.parse(JSON.stringify(theValue)); //copy the value, not the pointer if array or object
}
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 , theValue);
}
return returnArray; //return here if there are still more array dimensions
}