How can I loop through an array of string values and output all combinations for N number of string values?
I also need to preserve each combination so that I can loop through those for creating records in a database.
I found a combination generator that helps require a minimum and I modified it as follows:
var combine = function(a, min) {
var fn = function(n, src, got, all) {
if (n == 0) {
if (got.length > 0) {
all[all.length] = got;
}
return;
}
for (var j = 0; j < src.length; j++) {
fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
}
return;
}
var all = [];
for (var i = min; i < a.length; i++) {
fn(i, a, [], all);
}
all.push(a);
return all;
}
var subsets = combine(["RT","PT","DDA"], 1);
This outputs:
RT,PT,DDA,RT,PT,RT,DDA,PT,DDA,RT,PT,DDA
Which technically has all of the correct outputs, but I need to break them up into unique combos so that it outputs like this:
[[RT],[PT],[DDA],[RT,PT],[RT,DDA],[PT,DDA],[RT,PT,DDA]]
Having an array of arrays would allow me to loop through using the index later on to create unique records in the database. That's my thought at least and I could really use some help as I am still trying to fully understand recursive functions.