Javascript 构建嵌套数组的递归算法

Javascript 构建嵌套数组的递归算法,javascript,recursion,Javascript,Recursion,我正在努力将其推广到任何深度(下面是深度4的示例): 到目前为止,我得到了类似的信息: 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

我正在努力将其推广到任何深度(下面是深度4的示例):

到目前为止,我得到了类似的信息:

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"]  ...
var结果=[];
功能布局(尺寸、深度、堆栈){
如果(curDepth==-1){
返回result.push(stack.slice(0));
}
//除了第一轮递归,这是错误的。
//不想把每个元素都推到堆栈上。
对于(变量i=0;i
这是错误的(它给一个参数赋值)。

你可以试试看

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;
}
函数过程(数组、堆栈){
堆栈=堆栈| |[];
变量数组=[],
值=数组[0];
if(arrays.length==1){
对于(变量i=0;i
函数过程(数组、堆栈){
堆栈=堆栈| |[];
变量数组=[],
值=数组[0];
if(arrays.length==1){
对于(变量i=0;i
测试

我喜欢这个问题。这里是提琴:(你必须查看控制台才能看到结果与你想要的输出匹配)

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=[“一”];
功能布局(尺寸、深度、位置、val){
如果(深度类型==“未定义”)深度=-1;
如果(typeof val==“未定义”)val=“”;
else val+=''+尺寸[深度][位置];
var-retval=[];

如果(深度+1)这个问题理解/回答起来有点棘手,尽管我相信您的许多读者都能做到(递归数组读取/构造)。你能不能把这句话换成一句问题陈述,让潜在的回答者知道预期的输入和输出是什么?检查一下,谢谢,欣赏完整的答案!这很有效(其他两个答案基本相同)。但是,它比较慢,可能是因为阵列切片和连接。嘿,Grallen,你能帮我回答我的问题吗?这有点类似于这个问题,我自己也无法解决。如果你有时间,你能试一试吗?谢谢
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"]  ...
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;
}
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);