3

I'm struggling to generalize this for any depth (example below with depth 4):

var listA = [1,2,3,4,5,6,7,8,9,10,11,12];
var listB = ["A", "B", "C", "D", "E"];
var listC = ["a", "b", "c", "d", "e"];
var listD = ["one"];

var result = [];
for (var listAi = 0; listAi < listA.length; listAi++) {
  var a = [];
  result.push(a);
  for (var listBi = 0; listBi < listB.length; listBi++) {
    var b = [];
    a.push(b);
    for (var listCi = 0; listCi < listC.length; listCi++) {  
      var c = [];
      b.push(c);
      for (var listDi = 0; listDi < listD.length; listDi++) {
        c.push(listA[listAi] + " " + listB[listBi] + " " + listC[listCi] + " " + listD[listDi]);
      }
    }
  }
}

where the correct result is

[
    [
        [
            ["1 A a one"],
            ["1 A b one"],
            ["1 A c one"],
            ["1 A d one"],
            ["1 A e one"]
        ],
        [
            ["1 B a one"],
            ["1 B b one"],
            ["1 B c one"],
            ["1 B d one"],
            ["1 B e one"]
        ],
        [
            ["1 C a one"],
            ["1 C b one"],
            ["1 C c one"],
            ["1 C d one"],
            ["1 C e one"]
        ],
        [
            ["1 D a one"],
            ["1 D b one"],
            ["1 D c one"],
            ["1 D d one"],
            ["1 D e one"]
        ],
        [
            ["1 E a one"],
            ["1 E b one"],
            ["1 E c one"],
            ["1 E d one"],
            ["1 E e one"]
        ]
    ],
    [
        [
            ["2 A a one"],
            ["2 A b one"],
            ["2 A c one"],
            ["2 A d one"],
            ["2 A e one"]
        ],
        [
            ["2 B a one"],
            ["2 B b one"],
            ["2 B c one"],
            ["2 B d one"],
            ["2 B e one"]
        ],
        [
            ["2 C a one"],
            ["2 C b one"],
            ["2 C c one"],
            ["2 C d one"],
            ["2 C e one"]
        ],
        [
            ["2 D a one"],
            ["2 D b one"],
            ["2 D c one"],
            ["2 D d one"],
            ["2 D e one"]
        ],
        [
            ["2 E a one"],
            ["2 E b one"],
            ["2 E c one"],
            ["2 E d one"],
            ["2 E e one"]
        ]
    ],
    [
        [
            ["3 A a one"],
            ["3 A b one"],
            ["3 A c one"],
            ["3 A d one"],
            ["3 A e one"]
        ],
        [
            ["3 B a one"],
            ["3 B b one"],
            ["3 B c one"],
            ["3 B d one"],
            ["3 B e one"]
        ],
        [
            ["3 C a one"],
            ["3 C b one"],
            ["3 C c one"],
            ["3 C d one"],
            ["3 C e one"]
        ],
        [
            ["3 D a one"],
            ["3 D b one"],
            ["3 D c one"],
            ["3 D d one"],
            ["3 D e one"]
        ],
        [
            ["3 E a one"],
            ["3 E b one"],
            ["3 E c one"],
            ["3 E d one"],
            ["3 E e one"]
        ]
    ],
    [
        [
            ["4 A a one"],
            ["4 A b one"],
            ["4 A c one"],
            ["4 A d one"],
            ["4 A e one"]
        ],
        [
            ["4 B a one"],
            ["4 B b one"],
            ["4 B c one"],
            ["4 B d one"],
            ["4 B e one"]
        ],
        [
            ["4 C a one"],
            ["4 C b one"],
            ["4 C c one"],
            ["4 C d one"],
            ["4 C e one"]
        ],
        [
            ["4 D a one"],
            ["4 D b one"],
            ["4 D c one"],
            ["4 D d one"],
            ["4 D e one"]
        ],
        [
            ["4 E a one"],
            ["4 E b one"],
            ["4 E c one"],
            ["4 E d one"],
            ["4 E e one"]
        ]
    ],
    [
        [
            ["5 A a one"],
            ["5 A b one"],
            ["5 A c one"],
            ["5 A d one"],
            ["5 A e one"]
        ],
        [
            ["5 B a one"],
            ["5 B b one"],
            ["5 B c one"],
            ["5 B d one"],
            ["5 B e one"]
        ],
        [
            ["5 C a one"],
            ["5 C b one"],
            ["5 C c one"],
            ["5 C d one"],
            ["5 C e one"]
        ],
        [
            ["5 D a one"],
            ["5 D b one"],
            ["5 D c one"],
            ["5 D d one"],
            ["5 D e one"]
        ],
        [
            ["5 E a one"],
            ["5 E b one"],
            ["5 E c one"],
            ["5 E d one"],
            ["5 E e one"]
        ]
    ],
    [
        [
            ["6 A a one"],
            ["6 A b one"],
            ["6 A c one"],
            ["6 A d one"],
            ["6 A e one"]
        ],
        [
            ["6 B a one"],
            ["6 B b one"],
            ["6 B c one"],
            ["6 B d one"],
            ["6 B e one"]
        ],
        [
            ["6 C a one"],
            ["6 C b one"],
            ["6 C c one"],
            ["6 C d one"],
            ["6 C e one"]
        ],
        [
            ["6 D a one"],
            ["6 D b one"],
            ["6 D c one"],
            ["6 D d one"],
            ["6 D e one"]
        ],
        [
            ["6 E a one"],
            ["6 E b one"],
            ["6 E c one"],
            ["6 E d one"],
            ["6 E e one"]
        ]
    ],
    [
        [
            ["7 A a one"],
            ["7 A b one"],
            ["7 A c one"],
            ["7 A d one"],
            ["7 A e one"]
        ],
        [
            ["7 B a one"],
            ["7 B b one"],
            ["7 B c one"],
            ["7 B d one"],
            ["7 B e one"]
        ],
        [
            ["7 C a one"],
            ["7 C b one"],
            ["7 C c one"],
            ["7 C d one"],
            ["7 C e one"]
        ],
        [
            ["7 D a one"],
            ["7 D b one"],
            ["7 D c one"],
            ["7 D d one"],
            ["7 D e one"]
        ],
        [
            ["7 E a one"],
            ["7 E b one"],
            ["7 E c one"],
            ["7 E d one"],
            ["7 E e one"]
        ]
    ],
    [
        [
            ["8 A a one"],
            ["8 A b one"],
            ["8 A c one"],
            ["8 A d one"],
            ["8 A e one"]
        ],
        [
            ["8 B a one"],
            ["8 B b one"],
            ["8 B c one"],
            ["8 B d one"],
            ["8 B e one"]
        ],
        [
            ["8 C a one"],
            ["8 C b one"],
            ["8 C c one"],
            ["8 C d one"],
            ["8 C e one"]
        ],
        [
            ["8 D a one"],
            ["8 D b one"],
            ["8 D c one"],
            ["8 D d one"],
            ["8 D e one"]
        ],
        [
            ["8 E a one"],
            ["8 E b one"],
            ["8 E c one"],
            ["8 E d one"],
            ["8 E e one"]
        ]
    ],
    [
        [
            ["9 A a one"],
            ["9 A b one"],
            ["9 A c one"],
            ["9 A d one"],
            ["9 A e one"]
        ],
        [
            ["9 B a one"],
            ["9 B b one"],
            ["9 B c one"],
            ["9 B d one"],
            ["9 B e one"]
        ],
        [
            ["9 C a one"],
            ["9 C b one"],
            ["9 C c one"],
            ["9 C d one"],
            ["9 C e one"]
        ],
        [
            ["9 D a one"],
            ["9 D b one"],
            ["9 D c one"],
            ["9 D d one"],
            ["9 D e one"]
        ],
        [
            ["9 E a one"],
            ["9 E b one"],
            ["9 E c one"],
            ["9 E d one"],
            ["9 E e one"]
        ]
    ],
    [
        [
            ["10 A a one"],
            ["10 A b one"],
            ["10 A c one"],
            ["10 A d one"],
            ["10 A e one"]
        ],
        [
            ["10 B a one"],
            ["10 B b one"],
            ["10 B c one"],
            ["10 B d one"],
            ["10 B e one"]
        ],
        [
            ["10 C a one"],
            ["10 C b one"],
            ["10 C c one"],
            ["10 C d one"],
            ["10 C e one"]
        ],
        [
            ["10 D a one"],
            ["10 D b one"],
            ["10 D c one"],
            ["10 D d one"],
            ["10 D e one"]
        ],
        [
            ["10 E a one"],
            ["10 E b one"],
            ["10 E c one"],
            ["10 E d one"],
            ["10 E e one"]
        ]
    ],
    [
        [
            ["11 A a one"],
            ["11 A b one"],
            ["11 A c one"],
            ["11 A d one"],
            ["11 A e one"]
        ],
        [
            ["11 B a one"],
            ["11 B b one"],
            ["11 B c one"],
            ["11 B d one"],
            ["11 B e one"]
        ],
        [
            ["11 C a one"],
            ["11 C b one"],
            ["11 C c one"],
            ["11 C d one"],
            ["11 C e one"]
        ],
        [
            ["11 D a one"],
            ["11 D b one"],
            ["11 D c one"],
            ["11 D d one"],
            ["11 D e one"]
        ],
        [
            ["11 E a one"],
            ["11 E b one"],
            ["11 E c one"],
            ["11 E d one"],
            ["11 E e one"]
        ]
    ],
    [
        [
            ["12 A a one"],
            ["12 A b one"],
            ["12 A c one"],
            ["12 A d one"],
            ["12 A e one"]
        ],
        [
            ["12 B a one"],
            ["12 B b one"],
            ["12 B c one"],
            ["12 B d one"],
            ["12 B e one"]
        ],
        [
            ["12 C a one"],
            ["12 C b one"],
            ["12 C c one"],
            ["12 C d one"],
            ["12 C e one"]
        ],
        [
            ["12 D a one"],
            ["12 D b one"],
            ["12 D c one"],
            ["12 D d one"],
            ["12 D e one"]
        ],
        [
            ["12 E a one"],
            ["12 E b one"],
            ["12 E c one"],
            ["12 E d one"],
            ["12 E e one"]
        ]
    ]
]

I've got something like this so far:

var result = [];
function layout(dimensions, curDepth, stack) {
  if (curDepth === -1) {
    return result.push(stack.slice(0));
  }

  // for all but the first bout of recursion, this is wrong.
  // don't want to be pushing every element onto the stack.
  for (var i = 0; i < dimensions[curDepth].length; i++) {
    if (curDepth === 3) stack = []; // temporarily hardcoded to 3
    stack.push(dimensions[curDepth][i]);
    layout(dimensions, curDepth - 1, stack);
  }

}

layout([listD, listC, listB, listA], 3);

// [[1,"A","a","one"],[1,"A","a","one","b","one"],[1,"A","a","one","b","one","c","one"]  ...

which is wrong (and it makes an assignment to an argument).

2
  • 1
    this question is a little tricky to understand/answer, although I'm sure many of your readers are able to do it (recursive array reading/construction). Could you perhaps rephraase this into more of a problem statement, such that potential answerers know what the expected inputs and outputs are? Commented May 25, 2015 at 8:54
  • 1
    check jsfiddle.net/arunpjohny/22e6ws1s/4 Commented May 25, 2015 at 9:00

2 Answers 2

1

I liked this question. Here is the fiddle: https://jsfiddle.net/gcbo2aok/1/ (You'll have to view the console to see that the results match your desired output)

var listA = [1,2,3,4,5,6,7,8,9,10,11,12];
var listB = ["A", "B", "C", "D", "E"];
var listC = ["a", "b", "c", "d", "e"];
var listD = ["one"];

function layout(dimentions, depth, position, val){
    if(typeof depth === "undefined") depth = -1;
    if(typeof val === "undefined") val = "";
    else val += " " + dimentions[depth][position];
    var retval = [];
    if(depth+1<dimentions.length){
        for(var i = 0; i < dimentions[depth+1].length; i++){
                retval.push(layout(dimentions, depth+1, i, val));
        }
    }else{
        return val;
    }    
    return retval;    
};

retval = layout([listA, listB, listC, listD]);
console.log(retval);
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Grallen could you help me with my question its a little bit similar to this question and i cant figure it out on my own can you give it a shot if you have time thanks stackoverflow.com/questions/53473623/…
1

You can try

function process(arrays, stack) {
    stack = stack || [];
    var array = [],
        value = arrays[0];
    if (arrays.length == 1) {
        for (var i = 0; i < value.length; i++) {
            array.push(stack.concat(value[i]).join(' '))
        }
    } else if (arrays.length) {
        for (var i = 0; i < value.length; i++) {
            array.push(process(arrays.slice(1), stack.concat(value[i])))
        }
    }

    return array;
}

function process(arrays, stack) {
  stack = stack || [];
  var array = [],
    value = arrays[0];
  if (arrays.length == 1) {
    for (var i = 0; i < value.length; i++) {
      array.push(stack.concat(value[i]).join(' '))
    }
  } else if (arrays.length) {
    for (var i = 0; i < value.length; i++) {
      array.push(process(arrays.slice(1), stack.concat(value[i])))
    }
  }

  return array;
}


function test() {
  var listA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
  var listB = ["A", "B", "C", "D", "E"];
  var listC = ["a", "b", "c", "d", "e"];
  var listD = ["one", 'two'];


  var result = process([listA, listB, listC, listD])
  console.log(JSON.stringify(result));
  output.innerHTML = JSON.stringify(result)
}
<button onclick="test()">Test</button>
<div id="output"></div>

1 Comment

Thanks, appreciate the complete answer! This works (as do the other two answers, which are all essentially the same). However, it's slower, likely from the array slicing and concatenation. jsperf.com/recursive-nested-arrays

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.