0

The following code will generate 10 arrays, each with 10 subarrays, each with 10 subarrays, each with 10 subarrays.

paths = [];

for (var i = 0, len_i = 10; i < len_i; ++i) { // 1st dimension
    paths.push([]);
    for (var j = 0, len_j = 10; j < len_j; ++j) { // 2nd dimension
        paths[i].push([]);
        for (var k = 0, len_k = 10; k < len_k; ++k) { // 3rd dimension
            paths[i][j].push([]);
            for (var l = 0, len_l = 10; l < len_l; ++l) { // 4th dimension
                paths[i][j][k].push([]);
                paths[i][j][k][l] = [];
            }
        }
    }
}

I will eventually need to do this with more dimensions and am curious to know if any ingenious developers out there can accomplish this with a function of the form:

function makePaths(quantityInEachArray, dimensions) {

  paths = [];

   quantityInEachArray = (typeof quantityInEachArray === "undefined") ? 10 : quantityInEachArray;
   dimensions = (typeof dimensions === "undefined") ? 4 : dimensions;

   // Do some magic

   return paths;

 }

That function, in its default form, would return the same thing as the for loops I demonstrated above.

I understand that this is not a standard practice but I am doing it for a very specific reason and need to test the performance of it.

How do I modify this code to produce nth dimensional arrays?

4
  • 2
    Have you considered recursion? Commented Apr 16, 2014 at 20:05
  • codegolf.stackexchange.com/questions/25329/… Commented Apr 16, 2014 at 20:06
  • It really helps to realize that JavaScript doesn't really have 'multi-dimensional arrays` in the same way that C would have them. Instead, you have arrays of arrays. Commented Apr 16, 2014 at 20:08
  • Couldn't this be done iteratively by making an array out of 10 copies (deep clones) of the current MD array, and doing that N times? Commented Apr 16, 2014 at 20:12

2 Answers 2

4

You can use recursive function:

function nthArray(n, l) {
    if(n < 1) return;
    var arr = new Array(l);
    for(var i=0; i<l; ++i)
        arr[i] = nthArray(n-1, l);
    return arr;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. For some reason, the thought of recursion slipped my mind but this answer is succinct and perfect.
0

A simple magic would be (without recursion):

paths = []
arrays = [paths]
for (var i = 0; i < dimensions; i++) {
    tmpArr = []
    for (var k = 0; k < arrays.length; k++) {
        for (var j = 0; j < size; j++) {
            val = []
            tmpArr.push(val)
            arrays[k].push(val)
        }
    }
    arrays = tmpArr
}

(I am not very fluent in javascript, you probably need to declare vars at the beginning and every thing, but that's the idea)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.