<head>
<title>js两个数组排列组合</title>
<script type="text/javascript">
var a = ["A","B","C"]
var b = ["1", "2","3"]
window.onload = function () {
var a1 = new objPL(a)//计算数组a的排列组合
var b1 = new objPL(b)//计算数组b的排列组合
var str = ""
var n = 0
//a1和b1的排列组合个数就是两者相乘(双层循环)
for (var i = 0 i < a1.length i++) {
for (var j = 0 j < b1.length j++) {
str += a1[i] + b1[j] + " "
n++
}
str += "<br/>"
}
document.write("共" + n + "个<br/>")
document.write(str)
}
//取数组的排列组合
function objPL(arr) {
this.a = arr
this.r = []
this.n = 0
//从M个数里面取N个(递归)
this.mGetN = function (curIndex, num, curSelect) {
if (num == 0) {
this.r[this.n++] = curSelect return
}
if (this.a.length - curIndex < num) return
this.mGetN(curIndex + 1, num - 1, curSelect + this.a[curIndex])
this.mGetN(curIndex + 1, num, curSelect)
}
for (var i = 1 i <= this.a.length i++) {
this.mGetN(0, i, "")
}
return this.r
}
</script>
</head>
<body></body>
</html>
["A","B","C"]与["1","2","3"]组合的结果:
1.两个普通的数组,但是要数组长度相同,想要数组A和数组B的下标对应
2.想要得到的结果
3、实现过程,主要是两个数组长度相同,用哪一个做循环都可以
这就是整一个实现过程