I'm trying to combine all elements of a multidimensional Array to have a combination of elements.
e.g. If I have
var array = [
['a'],
['1', '2', '3', '4'],
['A']
];
The result must look like:
["a1A", "a2A", "a3A", "a4A"]
I could realise this result with the following code:
var finalArray = [];
var f1 = 0;
for (var i = 0; i < array.length - 1; i ++) {
f1 = 0;
for (var j = 0; j < array[i].length; j ++) {
for (var k = 0; k < array[i + 1].length; k ++) {
if (finalArray[f1])
finalArray[f1] += array[i + 1][k];
else
finalArray[f1] = array[i][j] + array[i + 1][k];
f1 ++;
}
}
}
console.log(finalArray);
The Problem is if I add more elements to the first or last member, it does not work as expected.
for example this array
var array = [
['a', 'b'],
['1', '2', '3', '4'],
['A']
];
returns:
["a1A", "a2A", "a3A", "a4A", "b1", "b2", "b3", "b4"]
which should be:
["a1A", "a2A", "a3A", "a4A", "b1A", "b2A", "b3A", "b4A"]
Any help to fix my code is highly appreciated.