JavaScript Combinations of Arbitrary number of Arrays and their Indices
by jarosciak
HTML
<label id="res"></label>
JavaScript
var count = 0;
var words = [
['L[0]', 'L[1]'], 'L[2]'], 'L[3]'], 'L[4]'], 'L[5]'], 'L[6]'], 'L[7]'],
['L[0]', 'L[1]'], 'L[2]'], 'L[3]'], 'L[4]'], 'L[5]'], 'L[6]'], 'L[7]'],
['O[0]'],
['O[0]']
],
result = getCombinations(words);
function getCombinations(array) {
function iter(i, p) {
if (i === array.length) {
count++;
result.push(p); // <==================== use the items
document.getElementById('res').innerHTML += count + " - " + p + '<br>';
return;
}
array[i].forEach(function(a) {
iter(i + 1, p.concat(a));
});
}
var result = [];
iter(0, []);
return result;
}