用于构建嵌套数组的递归算法

时间:2015-05-25 08:21:36

标签: javascript recursion

我正在努力将此概括为任何深度(下面的例子深度为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]);
      }
    }
  }
}

正确的结果是

[
    [
        [
            ["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"]
        ]
    ]
]

到目前为止我有这样的事情:

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"]  ...

这是错误的(它会对参数进行赋值)。

2 个答案:

答案 0 :(得分:1)

你可以尝试

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;
}

&#13;
&#13;
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)
}
&#13;
<button onclick="test()">Test</button>
<div id="output"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我喜欢这个问题。这是小提琴:https://jsfiddle.net/gcbo2aok/1/(你必须查看控制台,看看结果是否符合你想要的输出)

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);