JS使用循环获得不定维度笛卡尔积
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]); } }
以上是关于JS使用循环获得不定维度笛卡尔积的主要内容,如果未能解决你的问题,请参考以下文章