Source Arrays:
var arr1 = ["a", "b"];
var arr2 = ["c"];
var arr3 = ["d", "e", "f"];
I can do permutations:(No duplicate)
["a", "c", "d"],
["b", "c", "d"],
["b", "c", "e"],
["b", "c", "f"],
["a", "c", "e"],
["a", "c", "f"]
But how can I get the results permutation like?
["a", "c"],
["a", "d"],
["a", "e"],
["a", "f"],
["b", "c"],
["b", "d"],
["b", "e"],
["b", "f"],
["c", "d"],
["c", "e"],
["c", "f"]
I only got my single array permutation snippet here
var arr3 = ['d', 'e', 'f'];
function permutation (list, n) {
var results = []
function _perm (list, n, res, start) {
if (res.length === n) {
return results.push(res.join(','))
}
if (start === list.length) { return }
_perm(list, n, res.slice(), start + 1)
res.push(list[start])
_perm(list, n, res, start + 1)
}
_perm(list, n, [], 0)
return results
}
console.log(permutation(arr3, 2)) // print ["e,f", "d,f", "d,e"]
Because of the source arrays could be unlimited, I need to combine and permute them at the same time. I would like to know what is the best to achieve like this:
var arr1 = ['a', 'b'];
var arr2 = ['c'];
var arr3 = ['d', 'e', 'f'];
...
var arrN = ['x', 'y', 'z'];
permutation([arr1, arr2, arr3, arr4], 2)
permutation([arr1, arr2, arr3, arr4], 3)
permutation([arr1, arr2, arr3, arr4], 4)
I really appreciate any helps.