0

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];
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • *Would using array.map work in this case?* <-- Have you tried it? Is there a specific question here? – Scott Marcus Nov 25 '19 at 01:54
  • I tried and failed a few times with array.map. I think I have a dyslexic to certain functions, map seems to be one of them. Can you use array.map in a recursive scenario as would be required for a function like this? – Vince Pelss Nov 26 '19 at 03:06

2 Answers2

0

If the question is how to map all the elements in every dimensions, you can create a mapArray function like this:

function mapArray(a, func) {
    if (a instanceof Array) {
        return a.map(subArray => mapArray(subArray, func))
    } else {
        return func(a)
    }
}

//create dummy [5,3,2] array
var test = JSON.parse('[[[0,0],[0,1],[0,2]],[[1,1],[1,2],[1,3]],[[2,2],[2,3],[2,4]],[[3,3],[3,4],[3,5]],[[4,4],[4,5],[4,6]]]');

var test2 = mapArray(test, item => item * 2)

console.log("initial array:");
console.log(JSON.stringify(test));
console.log("all items multiplied by 2:");
console.log(JSON.stringify(test2));
Vincent
  • 3,945
  • 3
  • 13
  • 25
  • Thanks. Every example of creating multidimensional arrays seems to start with hand typing the whole array, as per your example. I find this odd, what if the array was a 3567 x 543 x 3 array? I get it that JS does not have the ability to create that array like other languages. Even the old BASICS had DIM(7,67,5) to create a 7 x 67 x 5 array. So I created the function displayed in the question above and thought it might be better done using map, but I am not sure how. – Vince Pelss Nov 26 '19 at 02:55
  • I think I didn't understand your first question. Wasn't the question about how to apply map() to each and every cell of a multidimensional array? – Vincent Nov 26 '19 at 08:36
  • I could have been clearer. I meant 'create' one of any size easily using a function. I have a recursive function that works, but I haven't successfully created one with map. – Vince Pelss Nov 27 '19 at 14:32
0

Here is a solution with map :

//with what value you want to fill your array?
const defaultCellValue = null;

//the actual function
function DIM(dims) {
 return Array.apply(null, Array(dims[0])).map(() => dims.length==1 ? defaultCellValue: DIM(dims.slice(1)))
}

//test
console.log(JSON.stringify(DIM([5,3,2])));

Basically you create an array with the length of the first dimension, then you map each cell of that array to a recursive sub-array.

Vincent
  • 3,945
  • 3
  • 13
  • 25
  • Thanks! I have expanded the code for clarity (For myself and others who don't swim as easily in code). I still have difficulty with the Array.apply(), but they try to explain it here: https://stackoverflow.com/questions/25512771/what-is-array-apply-actually-doing function DIM(dims) { return Array.apply(null, Array(dims[0])).map( function() { if(dims.length == 1) {//at last dimention element return defaultCellValue; } else {//recurse to next dimentional element return DIM(dims.slice(1)); } } ); } – Vince Pelss Jan 20 '20 at 15:18