Title: Combinations of elements of multiple arrays
Tags: javascript;arrays;algorithm
Question: I have this type of data
var arr = [
["A", "AA"],
["B"],
["C", "CC", "CCC"]
I want to get combinations of all the elements within each array. for e.g.
A B CC
A B CCC
AA B CCC
Note the sequence of the words are same, like this should not be one of the combination B A C.
I tried a couple of logics but can't get what I am looking for. I can obtain all permutations and combinations of all the words, but that's not what I am looking for.
Please suggest
Comment: I am not expecting a fully working code, I can write it my self, what I need is just a hint in the right direction.
@Jack No, rows and columns are dynamic.

Comment: Please show what you've tried, we're not here to write it for you, we're here to help you fix your bugs.

Here is the accepted answer: You basically want to permute across multiple lists:
function permute(input)
var out = [];
(function permute_r(input, current) {
if (input.length === 0) {
out.push(current);
return;
var next = input.slice(1);
for (var i = 0, n = input[0].length; i != n; ++i) {
permute_r(next, current.concat([input[0][i]]));
}(input, []));
return out;
permute(arr);

Here is another answer: The problem can be solved recursively. That is: for the first array, you have, for each of the elements, the result of the combinations formed with the two other arrays.
Something like this could work:
function arrayCombine ( array ) {
if (array.length &gt; 1) {
var result = new Array();
//This combines all the arrays except the first
var otherCombs = arrayCombine ( array.slice(1) );
for ( var n = 0; n < array[0].length; n++ )
for ( var i = 0; i < otherCombs.length; i++ )
result.push ( array[0][n] + otherCombs[i] );
return result;
//If we have only one array, the result is the array itself, for it contains in itself all the combinations of one element that can be made
else return array[0];

Here is another answer: Make an array of indices (idx), each element corresponding to each row. Initial value 0.
Start with index i = 0
Do what you do with the current combination.
Increment idx[i]. If it's less than the length of the row, go to 2
Set idx[i] to zero
Increment i. If it's greater than the number of rows, terminate algorithm, otherwise go to 2

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:02/22/14', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript><arrays><algorithm>', 'Title': 'Combinations of elements of multiple arrays', 'ViewCount': 812, 'id': 21952437, 'user_AboutMe': '', 'user_AccountId': 1649871.0, 'user_DisplayName': 'Salman', 'user_Reputation': 9161.0, 'user_Views': 669.0, 'user_id': 1520671.0}}