JS使用循环获得不定维度笛卡尔积
使用循环获得不定维度笛卡尔积:
参考http://blog.csdn.net/buptdavid/article/details/45918647改造
function testing() { var d = [ ["a1", "a2", "a3"], ["b1", "b2"], ["c1", "c2", "c3", "c4"], ["d1", "d2", "d3", "d4", "d5"], ["e1", "e2", "e3", "e4", "e5","e6"], ]; var total = 1; for (var i = 0; i < d.length; i++) { total *= d[i].length; } var e = []; var itemLoopNum = 1; var loopPerItem = 1; var now = 1; for (var i = 0; i < d.length; i++) { now *= d[i].length; var index = 0; var currentSize = d[i].length; itemLoopNum = total / now; loopPerItem = total / (itemLoopNum * currentSize); var myIndex = 0; for (var j = 0; j < d[i].length; j++) { for (var z = 0 ; z < loopPerItem; z++) { if (myIndex == d[i].length) { myIndex = 0; } for (var k = 0; k < itemLoopNum; k++) { e[index] = (e[index] == null ? "" :e[index] + ",") + d[i][myIndex]; index++; } myIndex++ } } } for (var l = 0; l < e.length; l++) { console.log(e[l]); } }