The solution to this problem is to create a nested loop for each of the four sets of words.
I did this by flattenning the array, and then excluding any "words" that didn't have a length longer than one.
function buildSentences(){
let ar = [
['Training', 'für', 'die', 'Polizei'],
['Trainings', '', 'das', 'Polizisten'],
['Trainingseinheit', '', '', 'Militär'],
['', '', '', 'Sanität'],
['', '', '', 'Sanitäter'],
]
var numWords = ar[0].length
// Logger.log("DEBUG Number of Words="+numWords)
// build array of words
var words= new Array // flattened array
var word = new Array // array without blanks
// create array for each word set
for (var w = 0;w<numWords;w++){
// Logger.log("DEBUG: w="+w)
var words = ar.map(function(e){return e[w]})
word[w] = words.filter(word => word.length > 1);
// Logger.log(word[w]) // DEBUG
}
// create an array to hold each sentence
var sentence = []
// create an array to hold the complete results
var results=[]
// Create sentences
// created nested loops for each of the words
// word one
for (var a=0;a<word[0].length;a++){
var word1 = word[0][a]
// Logger.log("DEBUG: word1 = "+word1)
// word two
for (var b=0;b<word[1].length;b++){
var word2 = word[1][b]
// Logger.log("DEBUG: word2 = "+word2)
// word three
for (var c=0;c<word[2].length;c++){
var word3 = word[2][c]
// Logger.log("DEBUG: word3 = "+word3)
// word four
for (var d=0;d<word[3].length;d++){
var word4 = word[3][d]
// Logger.log("DEBUG: word4 = "+word4)
// build the sentence
sentence.push(word1)
sentence.push(word2)
sentence.push(word3)
sentence.push(word4)
// copy the completed sentence to the results array
results.push(sentence.join(' '))
// clear the senteance array for the next sentence
sentence = []
}
}
}
}
Logger.log("number of results:"+results.length)
Logger.log(results)
}
RESULTS
Training für die Polizei,
Training für die Polizisten,
Training für die Militär,
Training für die Sanität,
Training für die Sanitäter,
Training für das Polizei,
Training für das Polizisten,
Training für das Militär,
Training für das Sanität,
Training für das Sanitäter,
Trainings für die Polizei,
Trainings für die Polizisten,
Trainings für die Militär,
Trainings für die Sanität,
Trainings für die Sanitäter,
Trainings für das Polizei,
Trainings für das Polizisten,
Trainings für das Militär,
Trainings für das Sanität,
Trainings für das Sanitäter,
Trainingseinheit für die Polizei,
Trainingseinheit für die Polizisten,
Trainingseinheit für die Militär,
Trainingseinheit für die Sanität,
Trainingseinheit für die Sanitäter,
Trainingseinheit für das Polizei,
Trainingseinheit für das Polizisten,
Trainingseinheit für das Militär,
Trainingseinheit für das Sanität,
Trainingseinheit für das Sanitäter
ADDENDUM
Building the array for the referenced answer (createCombinations
)
function mod02(){
let ar = [
['Training', 'für', 'die', 'Polizei'],
['Trainings', '', 'das', 'Polizisten'],
['Trainingseinheit', '', '', 'Militär'],
['', '', '', 'Sanität'],
['', '', '', 'Sanitäter'],
]
var numWords = ar[0].length
var words = [], word = [], v = []
for (var w = 0;w<numWords;w++){
words = ar.map(function(e){return e[w]})
word[w] = words.filter(word => word.length > 1)
v.push(word[w])
}
// Logger.log(v) // DEBUG
}
This creates the following array:
[[Training, Trainings, Trainingseinheit], [für], [die, das], [Polizei, Polizisten, Militär, Sanität, Sanitäter]]